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.23 by greg, Thu Jan 18 23:58:29 1990 UTC vs.
Revision 2.12 by greg, Mon Apr 12 09:58:09 1993 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                                                          /* get input */
201          for ( ; i < argc; i += 2)
202                  addpicture(argv[i], argv[i+1]);
203                                                          /* fill in spaces */
204 <        if (fill&F_BACK)
205 <                backpicture();
204 >        if (fillo&F_BACK)
205 >                backpicture(fillfunc, fillsamp);
206          else
207 <                fillpicture();
207 >                fillpicture(fillfunc);
208                                                          /* close calculation */
209          caldone();
210                                                          /* add to header */
211          printargs(argc, argv, stdout);
212          if (gotvfile) {
213 <                printf(VIEWSTR);
213 >                fputs(VIEWSTR, stdout);
214                  fprintview(&ourview, stdout);
215 <                printf("\n");
215 >                putc('\n', stdout);
216          }
217          if (pixaspect < .99 || pixaspect > 1.01)
218                  fputaspect(pixaspect, stdout);
219          if (ourexp > 0 && (ourexp < .995 || ourexp > 1.005))
220                  fputexpos(ourexp, stdout);
221 <        printf("\n");
221 >        fputformat(COLRFMT, stdout);
222 >        putc('\n', stdout);
223                                                          /* write picture */
224          writepicture();
225                                                          /* write z file */
# Line 225 | Line 239 | userr:
239   headline(s)                             /* process header string */
240   char    *s;
241   {
242 <        static char     *altname[] = {"rview","rpict","pinterp",VIEWSTR,NULL};
229 <        register char   **an;
242 >        char    fmt[32];
243  
244 <        printf("\t%s", s);
244 >        if (isformat(s)) {
245 >                formatval(fmt, s);
246 >                wrongformat = strcmp(fmt, COLRFMT);
247 >                return;
248 >        }
249 >        putc('\t', stdout);
250 >        fputs(s, stdout);
251  
252          if (isexpos(s)) {
253                  theirexp *= exposval(s);
254                  return;
255          }
256 <        for (an = altname; *an != NULL; an++)
257 <                if (!strncmp(*an, s, strlen(*an))) {
239 <                        if (sscanview(&theirview, s+strlen(*an)) > 0)
240 <                                gotview++;
241 <                        break;
242 <                }
256 >        if (isview(s) && sscanview(&theirview, s) > 0)
257 >                gotview++;
258   }
259  
260  
261   addpicture(pfile, zspec)                /* add picture to output */
262   char    *pfile, *zspec;
263   {
249        extern double   atof();
264          FILE    *pfp;
265          int     zfd;
266          char    *err;
# Line 255 | Line 269 | char   *pfile, *zspec;
269          struct position *plast;
270          int     y;
271                                          /* open picture file */
272 <        if ((pfp = fopen(pfile, "r")) == NULL) {
273 <                perror(pfile);
260 <                exit(1);
261 <        }
272 >        if ((pfp = fopen(pfile, "r")) == NULL)
273 >                syserror(pfile);
274                                          /* get header with exposure and view */
275          theirexp = 1.0;
276          gotview = 0;
277          printf("%s:\n", pfile);
278 <        getheader(pfp, headline);
279 <        if (!gotview || fgetresolu(&thresolu, &tvresolu, pfp)
280 <                        != (YMAJOR|YDECR)) {
269 <                fprintf(stderr, "%s: picture view error\n", pfile);
278 >        getheader(pfp, headline, NULL);
279 >        if (wrongformat || !gotview || !fgetsresolu(&tresolu, pfp)) {
280 >                fprintf(stderr, "%s: picture format error\n", pfile);
281                  exit(1);
282          }
283          if (ourexp <= 0)
# Line 278 | Line 289 | char   *pfile, *zspec;
289                  exit(1);
290          }
291                                          /* compute transformation */
292 <        pixform(theirs2ours, &theirview, &ourview);
292 >        hasmatrix = pixform(theirs2ours, &theirview, &ourview);
293                                          /* allocate scanlines */
294 <        scanin = (COLR *)malloc(thresolu*sizeof(COLR));
295 <        zin = (float *)malloc(thresolu*sizeof(float));
296 <        plast = (struct position *)calloc(thresolu, sizeof(struct position));
294 >        scanin = (COLR *)malloc(scanlen(&tresolu)*sizeof(COLR));
295 >        zin = (float *)malloc(scanlen(&tresolu)*sizeof(float));
296 >        plast = (struct position *)calloc(scanlen(&tresolu),
297 >                        sizeof(struct position));
298          if (scanin == NULL || zin == NULL || plast == NULL)
299 <                syserror();
299 >                syserror(progname);
300                                          /* get z specification or file */
301          if ((zfd = open(zspec, O_RDONLY)) == -1) {
302                  double  zvalue;
303                  register int    x;
304 <                if (!isfloat(zspec) || (zvalue = atof(zspec)) <= 0.0) {
305 <                        perror(zspec);
306 <                        exit(1);
295 <                }
296 <                for (x = 0; x < thresolu; x++)
304 >                if (!isfloat(zspec) || (zvalue = atof(zspec)) <= 0.0)
305 >                        syserror(zspec);
306 >                for (x = scanlen(&tresolu); x-- > 0; )
307                          zin[x] = zvalue;
308          }
309                                          /* load image */
310 <        for (y = tvresolu-1; y >= 0; y--) {
311 <                if (freadcolrs(scanin, thresolu, pfp) < 0) {
310 >        for (y = 0; y < numscans(&tresolu); y++) {
311 >                if (freadcolrs(scanin, scanlen(&tresolu), pfp) < 0) {
312                          fprintf(stderr, "%s: read error\n", pfile);
313                          exit(1);
314                  }
315 <                if (zfd != -1 && read(zfd,(char *)zin,thresolu*sizeof(float))
316 <                                < thresolu*sizeof(float)) {
315 >                if (zfd != -1 && read(zfd,(char *)zin,
316 >                                scanlen(&tresolu)*sizeof(float))
317 >                                < scanlen(&tresolu)*sizeof(float)) {
318                          fprintf(stderr, "%s: read error\n", zspec);
319                          exit(1);
320                  }
# Line 325 | Line 336 | register VIEW  *vw1, *vw2;
336   {
337          double  m4t[4][4];
338  
339 +        if (vw1->type != VT_PER && vw1->type != VT_PAR)
340 +                return(0);
341 +        if (vw2->type != VT_PER && vw2->type != VT_PAR)
342 +                return(0);
343          setident4(xfmat);
344          xfmat[0][0] = vw1->hvec[0];
345          xfmat[0][1] = vw1->hvec[1];
# Line 352 | Line 367 | register VIEW  *vw1, *vw2;
367          m4t[2][2] = vw2->vdir[2];
368          m4t[3][2] = -DOT(vw2->vp,vw2->vdir);
369          multmat4(xfmat, xfmat, m4t);
370 +        return(1);
371   }
372  
373  
# Line 361 | Line 377 | COLR   *pline;
377   float   *zline;
378   struct position *lasty;         /* input/output */
379   {
380 <        extern double   sqrt();
365 <        double  pos[3];
380 >        FVECT   pos;
381          struct position lastx, newpos;
382          register int    x;
383  
384          lastx.z = 0;
385 <        for (x = thresolu-1; x >= 0; x--) {
386 <                pos[0] = (x+.5)/thresolu + theirview.hoff - .5;
372 <                pos[1] = (y+.5)/tvresolu + theirview.voff - .5;
385 >        for (x = scanlen(&tresolu); x-- > 0; ) {
386 >                pix2loc(pos, &tresolu, x, y);
387                  pos[2] = zline[x];
388 <                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) {
388 >                if (movepixel(pos) < 0) {
389                          lasty[x].z = lastx.z = 0;       /* mark invalid */
390                          continue;
391                  }
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;
392                  newpos.x = pos[0] * hresolu;
393                  newpos.y = pos[1] * vresolu;
394                  newpos.z = zline[x];
# Line 419 | Line 418 | double z;
418          register int    x, y;                   /* final position */
419  
420                                          /* compute vector p0p1 */
421 <        if (fill&F_FORE && ABS(p1->z-p0->z) <= zt) {
421 >        if (fillo&F_FORE && ABS(p1->z-p0->z) <= zt) {
422                  s1x = p1->x - p0->x;
423                  s1y = p1->y - p0->y;
424                  l1 = ABS(s1x);
# Line 430 | Line 429 | double z;
429                  p1isy = -1;
430          }
431                                          /* compute vector p0p2 */
432 <        if (fill&F_FORE && ABS(p2->z-p0->z) <= zt) {
432 >        if (fillo&F_FORE && ABS(p2->z-p0->z) <= zt) {
433                  s2x = p2->x - p0->x;
434                  s2y = p2->y - p0->y;
435                  if (p1isy == 1)
# Line 448 | Line 447 | double z;
447                  y1 = p0->y + c1*s1y/l1;
448                  for (c2 = l2; c2-- > 0; ) {
449                          x = x1 + c2*s2x/l2;
450 +                        if (x < 0 || x >= hresolu)
451 +                                continue;
452                          y = y1 + c2*s2y/l2;
453 +                        if (y < 0 || y >= vresolu)
454 +                                continue;
455                          if (zscan(y)[x] <= 0 || zscan(y)[x]-z
456                                                  > zeps*zscan(y)[x]) {
457                                  zscan(y)[x] = z;
# Line 459 | Line 462 | double z;
462   }
463  
464  
465 < backpicture()                           /* background fill algorithm */
465 > movepixel(pos)                          /* reposition image point */
466 > FVECT   pos;
467   {
468 +        FVECT   pt, direc;
469 +        
470 +        if (pos[2] <= 0)                /* empty pixel */
471 +                return(-1);
472 +        if (hasmatrix) {
473 +                pos[0] += theirview.hoff - .5;
474 +                pos[1] += theirview.voff - .5;
475 +                if (theirview.type == VT_PER) {
476 +                        if (normdist)   /* adjust for eye-ray distance */
477 +                                pos[2] /= sqrt( 1.
478 +                                        + pos[0]*pos[0]*theirview.hn2
479 +                                        + pos[1]*pos[1]*theirview.vn2 );
480 +                        pos[0] *= pos[2];
481 +                        pos[1] *= pos[2];
482 +                }
483 +                multp3(pos, pos, theirs2ours);
484 +                if (pos[2] <= 0)
485 +                        return(-1);
486 +                if (ourview.type == VT_PER) {
487 +                        pos[0] /= pos[2];
488 +                        pos[1] /= pos[2];
489 +                }
490 +                pos[0] += .5 - ourview.hoff;
491 +                pos[1] += .5 - ourview.voff;
492 +                return(0);
493 +        }
494 +        if (viewray(pt, direc, &theirview, pos[0], pos[1]) < 0)
495 +                return(-1);
496 +        pt[0] += direc[0]*pos[2];
497 +        pt[1] += direc[1]*pos[2];
498 +        pt[2] += direc[2]*pos[2];
499 +        viewloc(pos, &ourview, pt);
500 +        if (pos[2] <= 0)
501 +                return(-1);
502 +        return(0);
503 + }
504 +
505 +
506 + backpicture(fill, samp)                 /* background fill algorithm */
507 + int     (*fill)();
508 + int     samp;
509 + {
510          int     *yback, xback;
511          int     y;
466        COLR    pfill;
512          register int    x, i;
513                                                          /* get back buffer */
514          yback = (int *)malloc(hresolu*sizeof(int));
515          if (yback == NULL)
516 <                syserror();
516 >                syserror(progname);
517          for (x = 0; x < hresolu; x++)
518                  yback[x] = -2;
519          /*
# Line 510 | Line 555 | backpicture()                          /* background fill algorithm */
555                                                  xback = x-1;
556                                  }
557                                  /*
558 <                                 * Check to see if we have no background for
559 <                                 * this pixel.  If not, use background color.
558 >                                 * If we have no background for this pixel,
559 >                                 * use the given fill function.
560                                   */
561 <                                if (xback < 0 && yback[x] < 0) {
562 <                                        (*deffill)(x,y);
518 <                                        continue;
519 <                                }
561 >                                if (xback < 0 && yback[x] < 0)
562 >                                        goto fillit;
563                                  /*
564                                   * Compare, and use the background that is
565                                   * farther, unless one of them is next to us.
566 +                                 * If the background is too distant, call
567 +                                 * the fill function.
568                                   */
569                                  if ( yback[x] < 0
570                                          || (xback >= 0 && ABS(x-xback) <= 1)
571                                          || ( ABS(y-yback[x]) > 1
572                                                  && zscan(yback[x])[x]
573                                                  < zscan(y)[xback] ) ) {
574 +                                        if (samp > 0 && ABS(x-xback) >= samp)
575 +                                                goto fillit;
576                                          copycolr(pscan(y)[x],pscan(y)[xback]);
577                                          zscan(y)[x] = zscan(y)[xback];
578                                  } else {
579 +                                        if (samp > 0 && ABS(y-yback[x]) > samp)
580 +                                                goto fillit;
581                                          copycolr(pscan(y)[x],pscan(yback[x])[x]);
582                                          zscan(y)[x] = zscan(yback[x])[x];
583                                  }
584 +                                continue;
585 +                        fillit:
586 +                                (*fill)(x,y);
587 +                                if (fill == rcalfill) {         /* use it */
588 +                                        clearqueue();
589 +                                        xback = x;
590 +                                        yback[x] = y;
591 +                                }
592                          } else {                                /* full pixel */
593                                  yback[x] = -2;
594                                  xback = -2;
# Line 541 | Line 598 | backpicture()                          /* background fill algorithm */
598   }
599  
600  
601 < fillpicture()                           /* paint in empty pixels with default */
601 > fillpicture(fill)               /* paint in empty pixels using fill */
602 > int     (*fill)();
603   {
604          register int    x, y;
605  
606          for (y = 0; y < vresolu; y++)
607                  for (x = 0; x < hresolu; x++)
608                          if (zscan(y)[x] <= 0)
609 <                                (*deffill)(x,y);
609 >                                (*fill)(x,y);
610   }
611  
612  
# Line 556 | Line 614 | writepicture()                         /* write out picture */
614   {
615          int     y;
616  
617 <        fputresolu(YMAJOR|YDECR, hresolu, vresolu, stdout);
617 >        fprtresolu(hresolu, vresolu, stdout);
618          for (y = vresolu-1; y >= 0; y--)
619                  if (fwritecolrs(pscan(y), hresolu, stdout) < 0)
620 <                        syserror();
620 >                        syserror(progname);
621   }
622  
623  
624   writedistance(fname)                    /* write out z file */
625   char    *fname;
626   {
569        extern double   sqrt();
627          int     donorm = normdist && ourview.type == VT_PER;
628          int     fd;
629          int     y;
630          float   *zout;
631  
632 <        if ((fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1) {
633 <                perror(fname);
577 <                exit(1);
578 <        }
632 >        if ((fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1)
633 >                syserror(fname);
634          if (donorm
635          && (zout = (float *)malloc(hresolu*sizeof(float))) == NULL)
636 <                syserror();
636 >                syserror(progname);
637          for (y = vresolu-1; y >= 0; y--) {
638                  if (donorm) {
639                          double  vx, yzn2;
640                          register int    x;
641 <                        yzn2 = y - .5*(vresolu-1);
641 >                        yzn2 = (y+.5)/vresolu + ourview.voff - .5;
642                          yzn2 = 1. + yzn2*yzn2*ourview.vn2;
643                          for (x = 0; x < hresolu; x++) {
644 <                                vx = x - .5*(hresolu-1);
644 >                                vx = (x+.5)/hresolu + ourview.hoff - .5;
645                                  zout[x] = zscan(y)[x]
646                                          * sqrt(vx*vx*ourview.hn2 + yzn2);
647                          }
648                  } else
649                          zout = zscan(y);
650                  if (write(fd, (char *)zout, hresolu*sizeof(float))
651 <                                < hresolu*sizeof(float)) {
652 <                        perror(fname);
598 <                        exit(1);
599 <                }
651 >                                < hresolu*sizeof(float))
652 >                        syserror(fname);
653          }
654          if (donorm)
655                  free((char *)zout);
# Line 625 | Line 678 | int    x, y;
678   }
679  
680  
681 < calstart(prog, args)                    /* start fill calculation */
681 > calstart(prog, args)                    /* start fill calculation */
682   char    *prog, *args;
683   {
684          char    combuf[512];
685 <        int     p0[2], p1[2];
685 >        char    *argv[64];
686 >        int     rval;
687 >        register char   **wp, *cp;
688  
689          if (childpid != -1) {
690                  fprintf(stderr, "%s: too many calculations\n", progname);
691                  exit(1);
692          }
693 <        sprintf(combuf, prog, PACKSIZ, args);
694 <        if (pipe(p0) < 0 || pipe(p1) < 0)
695 <                syserror();
696 <        if ((childpid = vfork()) == 0) {        /* fork calculation */
697 <                close(p0[1]);
698 <                close(p1[0]);
699 <                if (p0[0] != 0) {
700 <                        dup2(p0[0], 0);
701 <                        close(p0[0]);
702 <                }
703 <                if (p1[1] != 1) {
704 <                        dup2(p1[1], 1);
650 <                        close(p1[1]);
651 <                }
652 <                execl("/bin/sh", "sh", "-c", combuf, 0);
653 <                perror("/bin/sh");
654 <                _exit(127);
693 >        strcpy(combuf, prog);
694 >        strcat(combuf, args);
695 >        cp = combuf;
696 >        wp = argv;
697 >        for ( ; ; ) {
698 >                while (isspace(*cp))    /* nullify spaces */
699 >                        *cp++ = '\0';
700 >                if (!*cp)               /* all done? */
701 >                        break;
702 >                *wp++ = cp;             /* add argument to list */
703 >                while (*++cp && !isspace(*cp))
704 >                        ;
705          }
706 <        if (childpid == -1)
707 <                syserror();
708 <        close(p0[0]);
709 <        close(p1[1]);
710 <        if ((psend = fdopen(p0[1], "w")) == NULL)
711 <                syserror();
712 <        if ((precv = fdopen(p1[0], "r")) == NULL)
713 <                syserror();
706 >        *wp = NULL;
707 >                                                /* start process */
708 >        if ((rval = open_process(PDesc, argv)) < 0)
709 >                syserror(progname);
710 >        if (rval == 0) {
711 >                fprintf(stderr, "%s: command not found\n", argv[0]);
712 >                exit(1);
713 >        }
714 >        packsiz = rval/(6*sizeof(float)) - 1;
715 >        if (packsiz > PACKSIZ)
716 >                packsiz = PACKSIZ;
717          queuesiz = 0;
718   }
719  
720  
721 < caldone()                               /* done with calculation */
721 > caldone()                               /* done with calculation */
722   {
670        int     pid;
671
723          if (childpid == -1)
724                  return;
725 <        clearqueue(1);
726 <        while ((pid = wait(0)) != -1 && pid != childpid)
676 <                ;
725 >        clearqueue();
726 >        close_process(PDesc);
727          childpid = -1;
728   }
729  
# Line 681 | Line 731 | caldone()                              /* done with calculation */
731   rcalfill(x, y)                          /* fill with ray-calculated pixel */
732   int     x, y;
733   {
734 <        if (queuesiz >= PACKSIZ)        /* flush queue if needed */
735 <                clearqueue(0);
734 >        if (queuesiz >= packsiz)        /* flush queue if needed */
735 >                clearqueue();
736                                          /* add position to queue */
737          queue[queuesiz][0] = x;
738          queue[queuesiz][1] = y;
# Line 690 | Line 740 | int    x, y;
740   }
741  
742  
743 < clearqueue(done)                        /* process queue */
694 < int     done;
743 > clearqueue()                            /* process queue */
744   {
745          FVECT   orig, dir;
746 <        float   fbuf[6];
746 >        float   fbuf[6*(PACKSIZ+1)];
747 >        register float  *fbp;
748          register int    i;
749  
750 +        if (queuesiz == 0)
751 +                return;
752 +        fbp = fbuf;
753          for (i = 0; i < queuesiz; i++) {
754                  viewray(orig, dir, &ourview,
755                                  (queue[i][0]+.5)/hresolu,
756                                  (queue[i][1]+.5)/vresolu);
757 <                fbuf[0] = orig[0]; fbuf[1] = orig[1]; fbuf[2] = orig[2];
758 <                fbuf[3] = dir[0]; fbuf[4] = dir[1]; fbuf[5] = dir[2];
706 <                fwrite((char *)fbuf, sizeof(float), 6, psend);
757 >                *fbp++ = orig[0]; *fbp++ = orig[1]; *fbp++ = orig[2];
758 >                *fbp++ = dir[0]; *fbp++ = dir[1]; *fbp++ = dir[2];
759          }
760 <        if ((done ? fclose(psend) : fflush(psend)) == EOF)
761 <                syserror();
760 >                                        /* mark end and get results */
761 >        bzero((char *)fbp, 6*sizeof(float));
762 >        if (process(PDesc, fbuf, fbuf, 4*sizeof(float)*queuesiz,
763 >                        6*sizeof(float)*(queuesiz+1)) !=
764 >                        4*sizeof(float)*queuesiz) {
765 >                fprintf(stderr, "%s: error reading from rtrace process\n",
766 >                                progname);
767 >                exit(1);
768 >        }
769 >        fbp = fbuf;
770          for (i = 0; i < queuesiz; i++) {
711                if (fread((char *)fbuf, sizeof(float), 4, precv) < 4) {
712                        fprintf(stderr, "%s: read error in clearqueue\n",
713                                        progname);
714                        exit(1);
715                }
771                  if (ourexp > 0 && ourexp != 1.0) {
772 <                        fbuf[0] *= ourexp;
773 <                        fbuf[1] *= ourexp;
774 <                        fbuf[2] *= ourexp;
772 >                        fbp[0] *= ourexp;
773 >                        fbp[1] *= ourexp;
774 >                        fbp[2] *= ourexp;
775                  }
776                  setcolr(pscan(queue[i][1])[queue[i][0]],
777 <                                fbuf[0], fbuf[1], fbuf[2]);
778 <                zscan(queue[i][1])[queue[i][0]] = fbuf[3];
777 >                                fbp[0], fbp[1], fbp[2]);
778 >                zscan(queue[i][1])[queue[i][0]] = fbp[3];
779 >                fbp += 4;
780          }
725        if (done)
726                fclose(precv);
781          queuesiz = 0;
782   }
783  
784  
785 < syserror()                      /* report error and exit */
785 > syserror(s)                     /* report error and exit */
786 > char    *s;
787   {
788 <        perror(progname);
788 >        perror(s);
789          exit(1);
790   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines