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.21 by greg, Wed Jan 17 15:12:37 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 <sys/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 -x %d %s"
31 > #define RTCOM           "rtrace -h- -ovl -fff "
32  
33   #define ABS(x)          ((x)>0?(x):-(x))
34  
# Line 46 | Line 46 | float  *ourzbuf;                       /* corresponding z-buffer */
46  
47   char    *progname;
48  
49 < int     fill = F_FORE|F_BACK;           /* selected fill algorithm */
49 > int     fillo = F_FORE|F_BACK;          /* selected fill options */
50 > int     fillsamp = 0;                   /* sample separation (0 == inf) */
51   extern int      backfill(), rcalfill(); /* fill functions */
52 < int     (*deffill)() = backfill;        /* selected fill function */
52 > int     (*fillfunc)() = backfill;       /* selected fill function */
53   COLR    backcolr = BLKCOLR;             /* background color */
54   double  backz = 0.0;                    /* background z value */
55   int     normdist = 1;                   /* normalized distance? */
# Line 56 | 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 70 | 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 87 | 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);
106 <                                fill = 0;
105 >                                check(3,NULL);
106 >                                fillo = 0;
107                                  break;
108                          case 'f':                               /* foreground */
109 <                                check(3,0);
110 <                                fill = F_FORE;
109 >                                check(3,NULL);
110 >                                fillo = F_FORE;
111                                  break;
112                          case 'b':                               /* background */
113 <                                check(3,0);
114 <                                fill = F_BACK;
113 >                                check(3,NULL);
114 >                                fillo = F_BACK;
115                                  break;
116                          case 'a':                               /* all */
117 <                                check(3,0);
118 <                                fill = F_FORE|F_BACK;
117 >                                check(3,NULL);
118 >                                fillo = F_FORE|F_BACK;
119                                  break;
120 +                        case 's':                               /* sample */
121 +                                check(3,"i");
122 +                                fillsamp = atoi(argv[++i]);
123 +                                break;
124                          case 'c':                               /* color */
125 <                                check(3,3);
126 <                                deffill = backfill;
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);
133 <                                deffill = backfill;
132 >                                check(3,"f");
133 >                                fillfunc = backfill;
134                                  backz = atof(argv[++i]);
135                                  break;
136                          case 'r':                               /* rtrace */
137 <                                check(3,1);
138 <                                deffill = rcalfill;
137 >                                check(3,"s");
138 >                                fillfunc = rcalfill;
139                                  calstart(RTCOM, argv[++i]);
140                                  break;
141                          default:
# Line 134 | 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);
160 <                        } 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 173 | 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 180 | 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]);
205                                                          /* fill in spaces */
206 <        if (fill&F_BACK)
207 <                backpicture();
206 >        if (fillo&F_BACK)
207 >                backpicture(fillfunc, fillsamp);
208          else
209 <                fillpicture();
209 >                fillpicture(fillfunc);
210                                                          /* close calculation */
211          caldone();
212                                                          /* add to header */
213          printargs(argc, argv, stdout);
214          if (gotvfile) {
215 <                printf(VIEWSTR);
215 >                fputs(VIEWSTR, stdout);
216                  fprintview(&ourview, stdout);
217 <                printf("\n");
217 >                putc('\n', stdout);
218          }
219          if (pixaspect < .99 || pixaspect > 1.01)
220                  fputaspect(pixaspect, stdout);
221          if (ourexp > 0 && (ourexp < .995 || ourexp > 1.005))
222                  fputexpos(ourexp, stdout);
223 <        printf("\n");
223 >        fputformat(COLRFMT, stdout);
224 >        putc('\n', stdout);
225                                                          /* write picture */
226          writepicture();
227                                                          /* write z file */
# Line 225 | Line 241 | userr:
241   headline(s)                             /* process header string */
242   char    *s;
243   {
244 <        static char     *altname[] = {"rview","rpict","pinterp",VIEWSTR,NULL};
229 <        register char   **an;
244 >        char    fmt[32];
245  
246 <        printf("\t%s", s);
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  
255          if (isexpos(s)) {
256                  theirexp *= exposval(s);
257                  return;
258          }
259 <        for (an = altname; *an != NULL; an++)
260 <                if (!strncmp(*an, s, strlen(*an))) {
239 <                        if (sscanview(&theirview, s+strlen(*an)) > 0)
240 <                                gotview++;
241 <                        break;
242 <                }
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   {
249        extern double   atof();
267          FILE    *pfp;
268          int     zfd;
269          char    *err;
# Line 255 | 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);
260 <                exit(1);
261 <        }
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)) {
269 <                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 278 | Line 292 | char   *pfile, *zspec;
292                  exit(1);
293          }
294                                          /* compute transformation */
295 <        pixform(theirs2ours, &theirview, &ourview);
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);
295 <                }
296 <                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,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 325 | Line 339 | register VIEW  *vw1, *vw2;
339   {
340          double  m4t[4][4];
341  
342 +        if (vw1->type != VT_PER && vw1->type != VT_PAR)
343 +                return(0);
344 +        if (vw2->type != VT_PER && vw2->type != VT_PAR)
345 +                return(0);
346          setident4(xfmat);
347          xfmat[0][0] = vw1->hvec[0];
348          xfmat[0][1] = vw1->hvec[1];
# Line 352 | Line 370 | register VIEW  *vw1, *vw2;
370          m4t[2][2] = vw2->vdir[2];
371          m4t[3][2] = -DOT(vw2->vp,vw2->vdir);
372          multmat4(xfmat, xfmat, m4t);
373 +        return(1);
374   }
375  
376  
# Line 361 | Line 380 | COLR   *pline;
380   float   *zline;
381   struct position *lasty;         /* input/output */
382   {
383 <        extern double   sqrt();
365 <        double  pos[3];
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 + theirview.hoff - .5;
372 <                pos[1] = (y+.5)/tvresolu + theirview.voff - .5;
388 >        for (x = scanlen(&tresolu); x-- > 0; ) {
389 >                pix2loc(pos, &tresolu, x, y);
390                  pos[2] = zline[x];
391 <                if (theirview.type == VT_PER) {
375 <                        if (normdist)   /* adjust for eye-ray distance */
376 <                                pos[2] /= sqrt( 1.
377 <                                        + pos[0]*pos[0]*theirview.hn2
378 <                                        + pos[1]*pos[1]*theirview.vn2 );
379 <                        pos[0] *= pos[2];
380 <                        pos[1] *= pos[2];
381 <                }
382 <                multp3(pos, pos, theirs2ours);
383 <                if (pos[2] <= 0) {
391 >                if (movepixel(pos) < 0) {
392                          lasty[x].z = lastx.z = 0;       /* mark invalid */
393                          continue;
394                  }
387                if (ourview.type == VT_PER) {
388                        pos[0] /= pos[2];
389                        pos[1] /= pos[2];
390                }
391                pos[0] += .5 - ourview.hoff;
392                pos[1] += .5 - ourview.voff;
395                  newpos.x = pos[0] * hresolu;
396                  newpos.y = pos[1] * vresolu;
397                  newpos.z = zline[x];
# Line 419 | Line 421 | double z;
421          register int    x, y;                   /* final position */
422  
423                                          /* compute vector p0p1 */
424 <        if (fill&F_FORE && ABS(p1->z-p0->z) <= zt) {
424 >        if (fillo&F_FORE && ABS(p1->z-p0->z) <= zt) {
425                  s1x = p1->x - p0->x;
426                  s1y = p1->y - p0->y;
427                  l1 = ABS(s1x);
# Line 430 | Line 432 | double z;
432                  p1isy = -1;
433          }
434                                          /* compute vector p0p2 */
435 <        if (fill&F_FORE && ABS(p2->z-p0->z) <= zt) {
435 >        if (fillo&F_FORE && ABS(p2->z-p0->z) <= zt) {
436                  s2x = p2->x - p0->x;
437                  s2y = p2->y - p0->y;
438                  if (p1isy == 1)
# Line 448 | Line 450 | double z;
450                  y1 = p0->y + c1*s1y/l1;
451                  for (c2 = l2; c2-- > 0; ) {
452                          x = x1 + c2*s2x/l2;
453 +                        if (x < 0 || x >= hresolu)
454 +                                continue;
455                          y = y1 + c2*s2y/l2;
456 +                        if (y < 0 || y >= vresolu)
457 +                                continue;
458                          if (zscan(y)[x] <= 0 || zscan(y)[x]-z
459                                                  > zeps*zscan(y)[x]) {
460                                  zscan(y)[x] = z;
# Line 459 | Line 465 | double z;
465   }
466  
467  
468 < backpicture()                           /* background fill algorithm */
468 > movepixel(pos)                          /* reposition image point */
469 > 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;
478 +                if (theirview.type == VT_PER) {
479 +                        if (normdist)   /* adjust for eye-ray distance */
480 +                                pos[2] /= sqrt( 1.
481 +                                        + pos[0]*pos[0]*theirview.hn2
482 +                                        + pos[1]*pos[1]*theirview.vn2 );
483 +                        pos[0] *= pos[2];
484 +                        pos[1] *= pos[2];
485 +                }
486 +                multp3(pos, pos, theirs2ours);
487 +                if (pos[2] <= 0)
488 +                        return(-1);
489 +                if (ourview.type == VT_PER) {
490 +                        pos[0] /= pos[2];
491 +                        pos[1] /= pos[2];
492 +                }
493 +                pos[0] += .5 - ourview.hoff;
494 +                pos[1] += .5 - ourview.voff;
495 +                return(0);
496 +        }
497 +        if (viewray(pt, direc, &theirview, pos[0], pos[1]) < 0)
498 +                return(-1);
499 +        pt[0] += direc[0]*pos[2];
500 +        pt[1] += direc[1]*pos[2];
501 +        pt[2] += direc[2]*pos[2];
502 +        viewloc(pos, &ourview, pt);
503 +        if (pos[2] <= 0)
504 +                return(-1);
505 +        return(0);
506 + }
507 +
508 +
509 + backpicture(fill, samp)                 /* background fill algorithm */
510 + int     (*fill)();
511 + int     samp;
512 + {
513          int     *yback, xback;
514          int     y;
466        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 510 | Line 558 | backpicture()                          /* background fill algorithm */
558                                                  xback = x-1;
559                                  }
560                                  /*
561 <                                 * Check to see if we have no background for
562 <                                 * this pixel.  If not, use background color.
561 >                                 * If we have no background for this pixel,
562 >                                 * use the given fill function.
563                                   */
564 <                                if (xback < 0 && yback[x] < 0) {
565 <                                        (*deffill)(x,y);
518 <                                        continue;
519 <                                }
564 >                                if (xback < 0 && yback[x] < 0)
565 >                                        goto fillit;
566                                  /*
567                                   * Compare, and use the background that is
568                                   * farther, unless one of them is next to us.
569 +                                 * If the background is too distant, call
570 +                                 * the fill function.
571                                   */
572                                  if ( yback[x] < 0
573                                          || (xback >= 0 && ABS(x-xback) <= 1)
574                                          || ( ABS(y-yback[x]) > 1
575                                                  && zscan(yback[x])[x]
576                                                  < zscan(y)[xback] ) ) {
577 +                                        if (samp > 0 && ABS(x-xback) >= samp)
578 +                                                goto fillit;
579                                          copycolr(pscan(y)[x],pscan(y)[xback]);
580                                          zscan(y)[x] = zscan(y)[xback];
581                                  } else {
582 +                                        if (samp > 0 && ABS(y-yback[x]) > samp)
583 +                                                goto fillit;
584                                          copycolr(pscan(y)[x],pscan(yback[x])[x]);
585                                          zscan(y)[x] = zscan(yback[x])[x];
586                                  }
587 +                                continue;
588 +                        fillit:
589 +                                (*fill)(x,y);
590 +                                if (fill == rcalfill) {         /* use it */
591 +                                        clearqueue();
592 +                                        xback = x;
593 +                                        yback[x] = y;
594 +                                }
595                          } else {                                /* full pixel */
596                                  yback[x] = -2;
597                                  xback = -2;
# Line 541 | Line 601 | backpicture()                          /* background fill algorithm */
601   }
602  
603  
604 < fillpicture()                           /* paint in empty pixels with default */
604 > fillpicture(fill)               /* paint in empty pixels using fill */
605 > int     (*fill)();
606   {
607          register int    x, y;
608  
609          for (y = 0; y < vresolu; y++)
610                  for (x = 0; x < hresolu; x++)
611                          if (zscan(y)[x] <= 0)
612 <                                (*deffill)(x,y);
612 >                                (*fill)(x,y);
613   }
614  
615  
# Line 556 | 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   {
569        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);
577 <                exit(1);
578 <        }
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;
643                          register int    x;
644 <                        yzn2 = y - .5*(vresolu-1);
644 >                        yzn2 = (y+.5)/vresolu + ourview.voff - .5;
645                          yzn2 = 1. + yzn2*yzn2*ourview.vn2;
646                          for (x = 0; x < hresolu; x++) {
647 <                                vx = x - .5*(hresolu-1);
647 >                                vx = (x+.5)/hresolu + ourview.hoff - .5;
648                                  zout[x] = zscan(y)[x]
649                                          * sqrt(vx*vx*ourview.hn2 + yzn2);
650                          }
651                  } else
652                          zout = zscan(y);
653 <                if (write(fd, zout, hresolu*sizeof(float))
654 <                                < hresolu*sizeof(float)) {
655 <                        perror(fname);
598 <                        exit(1);
599 <                }
653 >                if (write(fd, (char *)zout, hresolu*sizeof(float))
654 >                                < hresolu*sizeof(float))
655 >                        syserror(fname);
656          }
657          if (donorm)
658                  free((char *)zout);
# Line 625 | 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, PACKSIZ, 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);
650 <                        close(p1[1]);
651 <                }
652 <                execl("/bin/sh", "sh", "-c", combuf, 0);
653 <                perror("/bin/sh");
654 <                _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   {
670        int     pid;
671
726          if (childpid == -1)
727                  return;
674        if (fclose(psend) == EOF)
675                syserror();
728          clearqueue();
729 <        fclose(precv);
678 <        while ((pid = wait(0)) != -1 && pid != childpid)
679 <                ;
729 >        close_process(PDesc);
730          childpid = -1;
731   }
732  
# Line 684 | Line 734 | caldone()                              /* done with calculation */
734   rcalfill(x, y)                          /* fill with ray-calculated pixel */
735   int     x, y;
736   {
737 <        FVECT   orig, dir;
688 <        float   outbuf[6];
689 <
690 <        if (queuesiz >= PACKSIZ) {      /* flush queue */
691 <                if (fflush(psend) == EOF)
692 <                        syserror();
737 >        if (queuesiz >= packsiz)        /* flush queue if needed */
738                  clearqueue();
739 <        }
695 <                                        /* send new ray */
696 <        viewray(orig, dir, &ourview, (x+.5)/hresolu, (y+.5)/vresolu);
697 <        outbuf[0] = orig[0]; outbuf[1] = orig[1]; outbuf[2] = orig[2];
698 <        outbuf[3] = dir[0]; outbuf[4] = dir[1]; outbuf[5] = dir[2];
699 <        if (fwrite(outbuf, sizeof(float), 6, psend) < 6)
700 <                syserror();
701 <                                        /* remember it */
739 >                                        /* add position to queue */
740          queue[queuesiz][0] = x;
741          queue[queuesiz][1] = y;
742          queuesiz++;
743   }
744  
745  
746 < clearqueue()                            /* get results from queue */
746 > clearqueue()                            /* process queue */
747   {
748 <        float   inbuf[4];
748 >        FVECT   orig, dir;
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 <                if (fread(inbuf, sizeof(float), 4, precv) < 4) {
758 <                        fprintf(stderr, "%s: read error in clearqueue\n",
759 <                                        progname);
760 <                        exit(1);
761 <                }
757 >                viewray(orig, dir, &ourview,
758 >                                (queue[i][0]+.5)/hresolu,
759 >                                (queue[i][1]+.5)/vresolu);
760 >                *fbp++ = orig[0]; *fbp++ = orig[1]; *fbp++ = orig[2];
761 >                *fbp++ = dir[0]; *fbp++ = dir[1]; *fbp++ = dir[2];
762 >        }
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++) {
774                  if (ourexp > 0 && ourexp != 1.0) {
775 <                        inbuf[0] *= ourexp;
776 <                        inbuf[1] *= ourexp;
777 <                        inbuf[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 <                                inbuf[0], inbuf[1], inbuf[2]);
781 <                zscan(queue[i][1])[queue[i][0]] = inbuf[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