ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pinterp.c
(Generate patch)

Comparing ray/src/px/pinterp.c (file contents):
Revision 1.33 by greg, Thu Apr 18 14:35:26 1991 UTC vs.
Revision 2.19 by greg, Fri Dec 23 22:35:12 1994 UTC

# Line 1 | Line 1
1 + /* Copyright (c) 1994 Regents of the University of California */
2 +
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
5   #endif
# Line 10 | Line 12 | static char SCCSid[] = "$SunId$ LBL";
12  
13   #include "standard.h"
14  
15 < #include <fcntl.h>
15 > #include <ctype.h>
16  
17   #include "view.h"
18  
19   #include "color.h"
20  
21 < #ifndef BSD
20 < #define vfork           fork
21 < #endif
21 > #include "resolu.h"
22  
23   #define pscan(y)        (ourpict+(y)*hresolu)
24 + #define sscan(y)        (ourspict+(y)*hresolu)
25 + #define wscan(y)        (ourweigh+(y)*hresolu)
26   #define zscan(y)        (ourzbuf+(y)*hresolu)
27 + #define averaging       (ourweigh != NULL)
28  
29 + #define MAXWT           100.            /* maximum pixel weight (averaging) */
30 +
31   #define F_FORE          1               /* fill foreground */
32   #define F_BACK          2               /* fill background */
33  
34 < #define PACKSIZ         42              /* calculation packet size */
34 > #define PACKSIZ         256             /* max. calculation packet size */
35  
36 < #define RTCOM           "rtrace -h -ovl -fff %s"
36 > #define RTCOM           "rtrace -h- -ovl -fff "
37  
38   #define ABS(x)          ((x)>0?(x):-(x))
39  
40   struct position {int x,y; float z;};
41  
42 + #define NSTEPS          64              /* number steps in overlap prescan */
43 + #define MINSTEP         4               /* minimum worthwhile preview step */
44 +
45 + struct bound {int min,max;};
46 +
47   VIEW    ourview = STDVIEW;              /* desired view */
48   int     hresolu = 512;                  /* horizontal resolution */
49   int     vresolu = 512;                  /* vertical resolution */
# Line 41 | Line 51 | double pixaspect = 1.0;                /* pixel aspect ratio */
51  
52   double  zeps = .02;                     /* allowed z epsilon */
53  
54 < COLR    *ourpict;                       /* output picture */
54 > COLR    *ourpict;                       /* output picture (COLR's) */
55 > COLOR   *ourspict;                      /* output pixel sums (-a option) */
56 > float   *ourweigh = NULL;               /* output pixel weights (-a option) */
57   float   *ourzbuf;                       /* corresponding z-buffer */
58  
59   char    *progname;
# Line 51 | Line 63 | int    fillsamp = 0;                   /* sample separation (0 == inf) */
63   extern int      backfill(), rcalfill(); /* fill functions */
64   int     (*fillfunc)() = backfill;       /* selected fill function */
65   COLR    backcolr = BLKCOLR;             /* background color */
66 + COLOR   backcolor = BLKCOLOR;           /* background color (float) */
67   double  backz = 0.0;                    /* background z value */
68   int     normdist = 1;                   /* normalized distance? */
69   double  ourexp = -1;                    /* output picture exposure */
# Line 58 | Line 71 | double ourexp = -1;                    /* output picture exposure */
71   VIEW    theirview = STDVIEW;            /* input view */
72   int     gotview;                        /* got input view? */
73   int     wrongformat = 0;                /* input in another format? */
74 < int     thresolu, tvresolu;             /* input resolution */
74 > RESOLU  tresolu;                        /* input resolution */
75   double  theirexp;                       /* input picture exposure */
76 < double  theirs2ours[4][4];              /* transformation matrix */
76 > MAT4    theirs2ours;                    /* transformation matrix */
77   int     hasmatrix = 0;                  /* has transformation matrix */
78  
79 < int     childpid = -1;                  /* id of fill process */
80 < FILE    *psend, *precv;                 /* pipes to/from fill calculation */
81 < int     queue[PACKSIZ][2];              /* pending pixels */
79 > int     PDesc[3] = {-1,-1,-1};          /* rtrace process descriptor */
80 > #define childpid        (PDesc[2])
81 > unsigned short  queue[PACKSIZ][2];      /* pending pixels */
82 > int     packsiz;                        /* actual packet size */
83   int     queuesiz;                       /* number of pixels pending */
84  
85 + extern double   movepixel();
86  
87 +
88   main(argc, argv)                        /* interpolate pictures */
89   int     argc;
90   char    *argv[];
91   {
92 < #define check(olen,narg)        if (argv[i][olen] || narg >= argc-i) goto badopt
93 <        extern double   atof();
92 > #define  check(ol,al)           if (argv[i][ol] || \
93 >                                badarg(argc-i-1,argv+i+1,al)) \
94 >                                goto badopt
95          int     gotvfile = 0;
96 +        int     doavg = -1;
97          char    *zfile = NULL;
98          char    *err;
99          int     i, rval;
# Line 90 | Line 108 | char   *argv[];
108                  }
109                  switch (argv[i][1]) {
110                  case 't':                               /* threshold */
111 <                        check(2,1);
111 >                        check(2,"f");
112                          zeps = atof(argv[++i]);
113                          break;
114 +                case 'a':                               /* average */
115 +                        check(2,NULL);
116 +                        doavg = 1;
117 +                        break;
118 +                case 'q':                               /* quick (no avg.) */
119 +                        check(2,NULL);
120 +                        doavg = 0;
121 +                        break;
122                  case 'n':                               /* dist. normalized? */
123 <                        check(2,0);
123 >                        check(2,NULL);
124                          normdist = !normdist;
125                          break;
126                  case 'f':                               /* fill type */
127                          switch (argv[i][2]) {
128                          case '0':                               /* none */
129 <                                check(3,0);
129 >                                check(3,NULL);
130                                  fillo = 0;
131                                  break;
132                          case 'f':                               /* foreground */
133 <                                check(3,0);
133 >                                check(3,NULL);
134                                  fillo = F_FORE;
135                                  break;
136                          case 'b':                               /* background */
137 <                                check(3,0);
137 >                                check(3,NULL);
138                                  fillo = F_BACK;
139                                  break;
140                          case 'a':                               /* all */
141 <                                check(3,0);
141 >                                check(3,NULL);
142                                  fillo = F_FORE|F_BACK;
143                                  break;
144                          case 's':                               /* sample */
145 <                                check(3,1);
145 >                                check(3,"i");
146                                  fillsamp = atoi(argv[++i]);
147                                  break;
148                          case 'c':                               /* color */
149 <                                check(3,3);
149 >                                check(3,"fff");
150                                  fillfunc = backfill;
151 <                                setcolr(backcolr, atof(argv[i+1]),
151 >                                setcolor(backcolor, atof(argv[i+1]),
152                                          atof(argv[i+2]), atof(argv[i+3]));
153 +                                setcolr(backcolr, colval(backcolor,RED),
154 +                                                colval(backcolor,GRN),
155 +                                                colval(backcolor,BLU));
156                                  i += 3;
157                                  break;
158                          case 'z':                               /* z value */
159 <                                check(3,1);
159 >                                check(3,"f");
160                                  fillfunc = backfill;
161                                  backz = atof(argv[++i]);
162                                  break;
163                          case 'r':                               /* rtrace */
164 <                                check(3,1);
164 >                                check(3,"s");
165                                  fillfunc = rcalfill;
166                                  calstart(RTCOM, argv[++i]);
167                                  break;
# Line 141 | Line 170 | char   *argv[];
170                          }
171                          break;
172                  case 'z':                               /* z file */
173 <                        check(2,1);
173 >                        check(2,"s");
174                          zfile = argv[++i];
175                          break;
176                  case 'x':                               /* x resolution */
177 <                        check(2,1);
177 >                        check(2,"i");
178                          hresolu = atoi(argv[++i]);
179                          break;
180                  case 'y':                               /* y resolution */
181 <                        check(2,1);
181 >                        check(2,"i");
182                          vresolu = atoi(argv[++i]);
183                          break;
184                  case 'p':                               /* pixel aspect */
185 <                        check(2,1);
185 >                        if (argv[i][2] != 'a')
186 >                                goto badopt;
187 >                        check(3,"f");
188                          pixaspect = atof(argv[++i]);
189                          break;
190                  case 'v':                               /* view file */
191                          if (argv[i][2] != 'f')
192                                  goto badopt;
193 <                        check(3,1);
193 >                        check(3,"s");
194                          gotvfile = viewfile(argv[++i], &ourview, 0, 0);
195 <                        if (gotvfile < 0) {
196 <                                perror(argv[i]);
197 <                                exit(1);
167 <                        } else if (gotvfile == 0) {
195 >                        if (gotvfile < 0)
196 >                                syserror(argv[i]);
197 >                        else if (gotvfile == 0) {
198                                  fprintf(stderr, "%s: bad view file\n",
199                                                  argv[i]);
200                                  exit(1);
# Line 180 | Line 210 | char   *argv[];
210                                                  /* check arguments */
211          if ((argc-i)%2)
212                  goto userr;
213 +        if (fillsamp == 1)
214 +                fillo &= ~F_BACK;
215                                                  /* set view */
216 <        if (err = setview(&ourview)) {
216 >        if ((err = setview(&ourview)) != NULL) {
217                  fprintf(stderr, "%s: %s\n", progname, err);
218                  exit(1);
219          }
220          normaspect(viewaspect(&ourview), &pixaspect, &hresolu, &vresolu);
221                                                  /* allocate frame */
222 <        ourpict = (COLR *)malloc(hresolu*vresolu*sizeof(COLR));
223 <        ourzbuf = (float *)calloc(hresolu*vresolu,sizeof(float));
224 <        if (ourpict == NULL || ourzbuf == NULL)
225 <                syserror();
222 >        if (doavg < 0)
223 >                doavg = (argc-i) > 2;
224 >        if (doavg) {
225 >                ourspict = (COLOR *)bmalloc(hresolu*vresolu*sizeof(COLOR));
226 >                ourweigh = (float *)bmalloc(hresolu*vresolu*sizeof(float));
227 >                if (ourspict == NULL | ourweigh == NULL)
228 >                        syserror(progname);
229 >        } else {
230 >                ourpict = (COLR *)bmalloc(hresolu*vresolu*sizeof(COLR));
231 >                if (ourpict == NULL)
232 >                        syserror(progname);
233 >        }
234 >        ourzbuf = (float *)bmalloc(hresolu*vresolu*sizeof(float));
235 >        if (ourzbuf == NULL)
236 >                syserror(progname);
237 >        bzero((char *)ourzbuf, hresolu*vresolu*sizeof(float));
238 >                                                        /* new header */
239 >        newheader("RADIANCE", stdout);
240                                                          /* get input */
241          for ( ; i < argc; i += 2)
242                  addpicture(argv[i], argv[i+1]);
# Line 201 | Line 247 | char   *argv[];
247                  fillpicture(fillfunc);
248                                                          /* close calculation */
249          caldone();
250 +                                                        /* aft clipping */
251 +        clipaft();
252                                                          /* add to header */
253          printargs(argc, argv, stdout);
254          if (gotvfile) {
# Line 233 | Line 281 | userr:
281   headline(s)                             /* process header string */
282   char    *s;
283   {
236        static char     *altname[] = {VIEWSTR,"rpict","rview","pinterp",NULL};
237        register char   **an;
284          char    fmt[32];
285  
286 <        if (isformat(s)) {
287 <                formatval(fmt, s);
286 >        if (isheadid(s))
287 >                return;
288 >        if (formatval(fmt, s)) {
289                  wrongformat = strcmp(fmt, COLRFMT);
290                  return;
291          }
# Line 249 | Line 296 | char   *s;
296                  theirexp *= exposval(s);
297                  return;
298          }
299 <        for (an = altname; *an != NULL; an++)
300 <                if (!strncmp(*an, s, strlen(*an))) {
254 <                        if (sscanview(&theirview, s+strlen(*an)) > 0)
255 <                                gotview++;
256 <                        break;
257 <                }
299 >        if (isview(s) && sscanview(&theirview, s) > 0)
300 >                gotview++;
301   }
302  
303  
304   addpicture(pfile, zspec)                /* add picture to output */
305   char    *pfile, *zspec;
306   {
264        extern double   atof();
307          FILE    *pfp;
308          int     zfd;
309          char    *err;
310          COLR    *scanin;
311          float   *zin;
312          struct position *plast;
313 +        struct bound    *xlim, ylim;
314          int     y;
315                                          /* open picture file */
316 <        if ((pfp = fopen(pfile, "r")) == NULL) {
317 <                perror(pfile);
275 <                exit(1);
276 <        }
316 >        if ((pfp = fopen(pfile, "r")) == NULL)
317 >                syserror(pfile);
318                                          /* get header with exposure and view */
319          theirexp = 1.0;
320          gotview = 0;
321          printf("%s:\n", pfile);
322          getheader(pfp, headline, NULL);
323 <        if (wrongformat || !gotview ||
283 <                fgetresolu(&thresolu, &tvresolu, pfp) != (YMAJOR|YDECR)) {
284 <
323 >        if (wrongformat || !gotview || !fgetsresolu(&tresolu, pfp)) {
324                  fprintf(stderr, "%s: picture format error\n", pfile);
325                  exit(1);
326          }
# Line 295 | Line 334 | char   *pfile, *zspec;
334          }
335                                          /* compute transformation */
336          hasmatrix = pixform(theirs2ours, &theirview, &ourview);
298                                        /* allocate scanlines */
299        scanin = (COLR *)malloc(thresolu*sizeof(COLR));
300        zin = (float *)malloc(thresolu*sizeof(float));
301        plast = (struct position *)calloc(thresolu, sizeof(struct position));
302        if (scanin == NULL || zin == NULL || plast == NULL)
303                syserror();
337                                          /* get z specification or file */
338 +        zin = (float *)malloc(scanlen(&tresolu)*sizeof(float));
339 +        if (zin == NULL)
340 +                syserror(progname);
341          if ((zfd = open(zspec, O_RDONLY)) == -1) {
342                  double  zvalue;
343                  register int    x;
344 <                if (!isfloat(zspec) || (zvalue = atof(zspec)) <= 0.0) {
345 <                        perror(zspec);
346 <                        exit(1);
311 <                }
312 <                for (x = 0; x < thresolu; x++)
344 >                if (!isflt(zspec) || (zvalue = atof(zspec)) <= 0.0)
345 >                        syserror(zspec);
346 >                for (x = scanlen(&tresolu); x-- > 0; )
347                          zin[x] = zvalue;
348          }
349 <                                        /* load image */
350 <        for (y = tvresolu-1; y >= 0; y--) {
351 <                if (freadcolrs(scanin, thresolu, pfp) < 0) {
349 >                                        /* compute transferrable perimeter */
350 >        xlim = (struct bound *)malloc(numscans(&tresolu)*sizeof(struct bound));
351 >        if (xlim == NULL)
352 >                syserror(progname);
353 >        if (!getperim(xlim, &ylim, zin, zfd)) { /* overlapping area? */
354 >                free((char *)zin);
355 >                free((char *)xlim);
356 >                if (zfd != -1)
357 >                        close(zfd);
358 >                fclose(pfp);
359 >                return;
360 >        }
361 >                                        /* allocate scanlines */
362 >        scanin = (COLR *)malloc(scanlen(&tresolu)*sizeof(COLR));
363 >        plast = (struct position *)calloc(scanlen(&tresolu),
364 >                        sizeof(struct position));
365 >        if (scanin == NULL | plast == NULL)
366 >                syserror(progname);
367 >                                        /* skip to starting point */
368 >        for (y = 0; y < ylim.min; y++)
369 >                if (freadcolrs(scanin, scanlen(&tresolu), pfp) < 0) {
370                          fprintf(stderr, "%s: read error\n", pfile);
371                          exit(1);
372                  }
373 <                if (zfd != -1 && read(zfd,(char *)zin,thresolu*sizeof(float))
374 <                                < thresolu*sizeof(float)) {
375 <                        fprintf(stderr, "%s: read error\n", zspec);
373 >        if (zfd != -1 && lseek(zfd,
374 >                        (long)ylim.min*scanlen(&tresolu)*sizeof(float), 0) < 0)
375 >                syserror(zspec);
376 >                                        /* load image */
377 >        for (y = ylim.min; y <= ylim.max; y++) {
378 >                if (freadcolrs(scanin, scanlen(&tresolu), pfp) < 0) {
379 >                        fprintf(stderr, "%s: read error\n", pfile);
380                          exit(1);
381                  }
382 <                addscanline(y, scanin, zin, plast);
382 >                if (zfd != -1 && read(zfd, (char *)zin,
383 >                                scanlen(&tresolu)*sizeof(float))
384 >                                < scanlen(&tresolu)*sizeof(float))
385 >                        syserror(zspec);
386 >                addscanline(xlim+y, y, scanin, zin, plast);
387          }
388                                          /* clean up */
389 +        free((char *)xlim);
390          free((char *)scanin);
391          free((char *)zin);
392          free((char *)plast);
# Line 336 | Line 397 | char   *pfile, *zspec;
397  
398  
399   pixform(xfmat, vw1, vw2)                /* compute view1 to view2 matrix */
400 < register double xfmat[4][4];
400 > register MAT4   xfmat;
401   register VIEW   *vw1, *vw2;
402   {
403          double  m4t[4][4];
# Line 376 | Line 437 | register VIEW  *vw1, *vw2;
437   }
438  
439  
440 < addscanline(y, pline, zline, lasty)     /* add scanline to output */
440 > addscanline(xl, y, pline, zline, lasty) /* add scanline to output */
441 > struct bound    *xl;
442   int     y;
443   COLR    *pline;
444   float   *zline;
445   struct position *lasty;         /* input/output */
446   {
385        extern double   sqrt();
447          FVECT   pos;
448          struct position lastx, newpos;
449 +        double  wt;
450          register int    x;
451  
452          lastx.z = 0;
453 <        for (x = thresolu-1; x >= 0; x--) {
454 <                pos[0] = (x+.5)/thresolu;
393 <                pos[1] = (y+.5)/tvresolu;
453 >        for (x = xl->max; x >= xl->min; x--) {
454 >                pix2loc(pos, &tresolu, x, y);
455                  pos[2] = zline[x];
456 <                if (movepixel(pos) < 0) {
456 >                if ((wt = movepixel(pos)) <= FTINY) {
457                          lasty[x].z = lastx.z = 0;       /* mark invalid */
458                          continue;
459                  }
460 +                                        /* add pixel to our image */
461                  newpos.x = pos[0] * hresolu;
462                  newpos.y = pos[1] * vresolu;
463                  newpos.z = zline[x];
464 <                                        /* add pixel to our image */
465 <                if (pos[0] >= 0 && newpos.x < hresolu
466 <                                && pos[1] >= 0 && newpos.y < vresolu) {
467 <                        addpixel(&newpos, &lastx, &lasty[x], pline[x], pos[2]);
406 <                        lasty[x].x = lastx.x = newpos.x;
407 <                        lasty[x].y = lastx.y = newpos.y;
408 <                        lasty[x].z = lastx.z = newpos.z;
409 <                } else
410 <                        lasty[x].z = lastx.z = 0;       /* mark invalid */
464 >                addpixel(&newpos, &lastx, &lasty[x], pline[x], wt, pos[2]);
465 >                lasty[x].x = lastx.x = newpos.x;
466 >                lasty[x].y = lastx.y = newpos.y;
467 >                lasty[x].z = lastx.z = newpos.z;
468          }
469   }
470  
471  
472 < addpixel(p0, p1, p2, pix, z)            /* fill in pixel parallelogram */
472 > addpixel(p0, p1, p2, pix, w, z)         /* fill in pixel parallelogram */
473   struct position *p0, *p1, *p2;
474   COLR    pix;
475 + double  w;
476   double  z;
477   {
478          double  zt = 2.*zeps*p0->z;             /* threshold */
479 +        COLOR   pval;                           /* converted+weighted pixel */
480          int     s1x, s1y, s2x, s2y;             /* step sizes */
481          int     l1, l2, c1, c2;                 /* side lengths and counters */
482          int     p1isy;                          /* p0p1 along y? */
# Line 431 | Line 490 | double z;
490                  l1 = ABS(s1x);
491                  if (p1isy = (ABS(s1y) > l1))
492                          l1 = ABS(s1y);
493 +                if (l1 < 1)
494 +                        l1 = 1;
495          } else {
496                  l1 = s1x = s1y = 1;
497                  p1isy = -1;
# Line 446 | Line 507 | double z;
507                          if (p1isy != 0 && ABS(s2x) > l2)
508                                  l2 = ABS(s2x);
509                  }
510 +                if (l2 < 1)
511 +                        l2 = 1;
512          } else
513                  l2 = s2x = s2y = 1;
514                                          /* fill the parallelogram */
515 +        if (averaging) {
516 +                colr_color(pval, pix);
517 +                scalecolor(pval, w);
518 +        }
519          for (c1 = l1; c1-- > 0; ) {
520                  x1 = p0->x + c1*s1x/l1;
521                  y1 = p0->y + c1*s1y/l1;
# Line 459 | Line 526 | double z;
526                          y = y1 + c2*s2y/l2;
527                          if (y < 0 || y >= vresolu)
528                                  continue;
529 <                        if (zscan(y)[x] <= 0 || zscan(y)[x]-z
529 >                        if (averaging) {
530 >                                if (zscan(y)[x] <= 0 || zscan(y)[x]-z
531                                                  > zeps*zscan(y)[x]) {
532 <                                zscan(y)[x] = z;
532 >                                        copycolor(sscan(y)[x], pval);
533 >                                        wscan(y)[x] = w;
534 >                                        zscan(y)[x] = z;
535 >                                } else if (z-zscan(y)[x] <= zeps*zscan(y)[x]) {
536 >                                        addcolor(sscan(y)[x], pval);
537 >                                        wscan(y)[x] += w;
538 >                                }
539 >                        } else if (zscan(y)[x] <= 0 || zscan(y)[x]-z
540 >                                                > zeps*zscan(y)[x]) {
541                                  copycolr(pscan(y)[x], pix);
542 +                                zscan(y)[x] = z;
543                          }
544                  }
545          }
546   }
547  
548  
549 + double
550   movepixel(pos)                          /* reposition image point */
551   FVECT   pos;
552   {
553 <        FVECT   pt, direc;
553 >        double  d0, d1;
554 >        FVECT   pt, tdir, odir;
555 >        register int    i;
556          
557          if (pos[2] <= 0)                /* empty pixel */
558 <                return(-1);
559 <        if (hasmatrix) {
558 >                return(0);
559 >        if (normdist && theirview.type == VT_PER) {     /* adjust distance */
560 >                d0 = pos[0] + theirview.hoff - .5;
561 >                d1 = pos[1] + theirview.voff - .5;
562 >                pos[2] /= sqrt(1. + d0*d0*theirview.hn2 + d1*d1*theirview.vn2);
563 >        }
564 >        if (!averaging && hasmatrix) {
565                  pos[0] += theirview.hoff - .5;
566                  pos[1] += theirview.voff - .5;
567                  if (theirview.type == VT_PER) {
483                        if (normdist)   /* adjust for eye-ray distance */
484                                pos[2] /= sqrt( 1.
485                                        + pos[0]*pos[0]*theirview.hn2
486                                        + pos[1]*pos[1]*theirview.vn2 );
568                          pos[0] *= pos[2];
569                          pos[1] *= pos[2];
570                  }
571                  multp3(pos, pos, theirs2ours);
572                  if (pos[2] <= 0)
573 <                        return(-1);
573 >                        return(0);
574                  if (ourview.type == VT_PER) {
575                          pos[0] /= pos[2];
576                          pos[1] /= pos[2];
577                  }
578                  pos[0] += .5 - ourview.hoff;
579                  pos[1] += .5 - ourview.voff;
580 +        } else {
581 +                if (viewray(pt, tdir, &theirview, pos[0], pos[1]) < -FTINY)
582 +                        return(0);
583 +                pt[0] += tdir[0]*pos[2];
584 +                pt[1] += tdir[1]*pos[2];
585 +                pt[2] += tdir[2]*pos[2];
586 +                viewloc(pos, &ourview, pt);
587 +                if (pos[2] <= 0)
588 +                        return(0);
589 +        }
590 +        if (pos[0] < 0 || pos[0] >= 1-FTINY || pos[1] < 0 || pos[1] >= 1-FTINY)
591                  return(0);
592 +        if (!averaging)
593 +                return(1);
594 +        if (ourview.type == VT_PAR)             /* compute our direction */
595 +                VCOPY(odir, ourview.vdir);
596 +        else
597 +                for (i = 0; i < 3; i++)
598 +                        odir[i] = (pt[i] - ourview.vp[i])/pos[2];
599 +        d0 = DOT(odir,tdir);                    /* compute pixel weight */
600 +        if (d0 <= FTINY)
601 +                return(0);              /* relative angle >= 90 degrees */
602 +        if (d0 >= 1.-1./MAXWT/MAXWT)
603 +                return(MAXWT);          /* clip to maximum weight */
604 +        return(1./sqrt(1.-d0));
605 + }
606 +
607 +
608 + getperim(xl, yl, zline, zfd)            /* compute overlapping image area */
609 + register struct bound   *xl;
610 + struct bound    *yl;
611 + float   *zline;
612 + int     zfd;
613 + {
614 +        int     step;
615 +        FVECT   pos;
616 +        register int    x, y;
617 +                                                /* set up step size */
618 +        if (scanlen(&tresolu) < numscans(&tresolu))
619 +                step = scanlen(&tresolu)/NSTEPS;
620 +        else
621 +                step = numscans(&tresolu)/NSTEPS;
622 +        if (step < MINSTEP) {                   /* not worth cropping? */
623 +                yl->min = 0;
624 +                yl->max = numscans(&tresolu) - 1;
625 +                x = scanlen(&tresolu) - 1;
626 +                for (y = numscans(&tresolu); y--; ) {
627 +                        xl[y].min = 0;
628 +                        xl[y].max = x;
629 +                }
630 +                return(1);
631          }
632 <        if (viewray(pt, direc, &theirview, pos[0], pos[1]) < 0)
633 <                return(-1);
634 <        pt[0] += direc[0]*pos[2];
635 <        pt[1] += direc[1]*pos[2];
636 <        pt[2] += direc[2]*pos[2];
637 <        viewpixel(&pos[0], &pos[1], &pos[2], &ourview, pt);
638 <        if (pos[2] <= 0)
639 <                return(-1);
640 <        return(0);
632 >        yl->min = 32000; yl->max = 0;           /* search for points on image */
633 >        for (y = step - 1; y < numscans(&tresolu); y += step) {
634 >                if (zfd != -1) {
635 >                        if (lseek(zfd, (long)y*scanlen(&tresolu)*sizeof(float),
636 >                                        0) < 0)
637 >                                syserror("lseek");
638 >                        if (read(zfd, (char *)zline,
639 >                                        scanlen(&tresolu)*sizeof(float))
640 >                                        < scanlen(&tresolu)*sizeof(float))
641 >                                syserror("read");
642 >                }
643 >                xl[y].min = 32000; xl[y].max = 0;               /* x max */
644 >                for (x = scanlen(&tresolu); (x -= step) > 0; ) {
645 >                        pix2loc(pos, &tresolu, x, y);
646 >                        pos[2] = zline[x];
647 >                        if (movepixel(pos) > FTINY) {
648 >                                xl[y].max = x + step - 1;
649 >                                xl[y].min = x - step + 1;       /* x min */
650 >                                if (xl[y].min < 0)
651 >                                        xl[y].min = 0;
652 >                                for (x = step - 1; x < xl[y].max; x += step) {
653 >                                        pix2loc(pos, &tresolu, x, y);
654 >                                        pos[2] = zline[x];
655 >                                        if (movepixel(pos) > FTINY) {
656 >                                                xl[y].min = x - step + 1;
657 >                                                break;
658 >                                        }
659 >                                }
660 >                                if (y < yl->min)                /* y limits */
661 >                                        yl->min = y - step + 1;
662 >                                yl->max = y + step - 1;
663 >                                break;
664 >                        }
665 >                }
666 >                                                        /* fill in between */
667 >                if (y < step) {
668 >                        xl[y-1].min = xl[y].min;
669 >                        xl[y-1].max = xl[y].max;
670 >                } else {
671 >                        if (xl[y].min < xl[y-step].min)
672 >                                xl[y-1].min = xl[y].min;
673 >                        else
674 >                                xl[y-1].min = xl[y-step].min;
675 >                        if (xl[y].max > xl[y-step].max)
676 >                                xl[y-1].max = xl[y].max;
677 >                        else
678 >                                xl[y-1].max = xl[y-step].max;
679 >                }
680 >                for (x = 2; x < step; x++)
681 >                        copystruct(xl+y-x, xl+y-1);
682 >        }
683 >        if (yl->max >= numscans(&tresolu))
684 >                yl->max = numscans(&tresolu) - 1;
685 >        for (x = numscans(&tresolu) - 1; x > y; x--)    /* fill bottom rows */
686 >                copystruct(xl+x, xl+y);
687 >        return(yl->max >= yl->min);
688   }
689  
690  
# Line 520 | Line 698 | int    samp;
698                                                          /* get back buffer */
699          yback = (int *)malloc(hresolu*sizeof(int));
700          if (yback == NULL)
701 <                syserror();
701 >                syserror(progname);
702          for (x = 0; x < hresolu; x++)
703                  yback[x] = -2;
704          /*
# Line 580 | Line 758 | int    samp;
758                                                  < zscan(y)[xback] ) ) {
759                                          if (samp > 0 && ABS(x-xback) >= samp)
760                                                  goto fillit;
761 <                                        copycolr(pscan(y)[x],pscan(y)[xback]);
761 >                                        if (averaging) {
762 >                                                copycolor(sscan(y)[x],
763 >                                                        sscan(y)[xback]);
764 >                                                wscan(y)[x] = wscan(y)[xback];
765 >                                        } else
766 >                                                copycolr(pscan(y)[x],
767 >                                                        pscan(y)[xback]);
768                                          zscan(y)[x] = zscan(y)[xback];
769                                  } else {
770                                          if (samp > 0 && ABS(y-yback[x]) > samp)
771                                                  goto fillit;
772 <                                        copycolr(pscan(y)[x],pscan(yback[x])[x]);
772 >                                        if (averaging) {
773 >                                                copycolor(sscan(y)[x],
774 >                                                        sscan(yback[x])[x]);
775 >                                                wscan(y)[x] =
776 >                                                        wscan(yback[x])[x];
777 >                                        } else
778 >                                                copycolr(pscan(y)[x],
779 >                                                        pscan(yback[x])[x]);
780                                          zscan(y)[x] = zscan(yback[x])[x];
781                                  }
782                                  continue;
# Line 617 | Line 808 | int    (*fill)();
808   }
809  
810  
811 + clipaft()                       /* perform aft clipping as indicated */
812 + {
813 +        register int    x, y;
814 +        double  tstdist;
815 +        double  yzn2, vx;
816 +
817 +        if (ourview.vaft <= FTINY)
818 +                return;
819 +        tstdist = ourview.vaft - ourview.vfore;
820 +        for (y = 0; y < vresolu; y++) {
821 +                if (ourview.type == VT_PER) {           /* adjust distance */
822 +                        yzn2 = (y+.5)/vresolu + ourview.voff - .5;
823 +                        yzn2 = 1. + yzn2*yzn2*ourview.vn2;
824 +                        tstdist = (ourview.vaft - ourview.vfore)*sqrt(yzn2);
825 +                }
826 +                for (x = 0; x < hresolu; x++)
827 +                        if (zscan(y)[x] > tstdist) {
828 +                                if (ourview.type == VT_PER) {
829 +                                        vx = (x+.5)/hresolu + ourview.hoff - .5;
830 +                                        if (zscan(y)[x] <= (ourview.vaft -
831 +                                                        ourview.vfore) *
832 +                                                sqrt(vx*vx*ourview.hn2 + yzn2))
833 +                                                continue;
834 +                                }
835 +                                if (averaging)
836 +                                        bzero(sscan(y)[x], sizeof(COLOR));
837 +                                else
838 +                                        bzero(pscan(y)[x], sizeof(COLR));
839 +                                zscan(y)[x] = 0.0;
840 +                        }
841 +        }
842 + }
843 +
844 +
845   writepicture()                          /* write out picture */
846   {
847          int     y;
848 +        register int    x;
849 +        double  d;
850  
851 <        fputresolu(YMAJOR|YDECR, hresolu, vresolu, stdout);
851 >        fprtresolu(hresolu, vresolu, stdout);
852          for (y = vresolu-1; y >= 0; y--)
853 <                if (fwritecolrs(pscan(y), hresolu, stdout) < 0)
854 <                        syserror();
853 >                if (averaging) {
854 >                        for (x = 0; x < hresolu; x++) { /* average pixels */
855 >                                d = 1./wscan(y)[x];
856 >                                scalecolor(sscan(y)[x], d);
857 >                        }
858 >                        if (fwritescan(sscan(y), hresolu, stdout) < 0)
859 >                                syserror(progname);
860 >                } else if (fwritecolrs(pscan(y), hresolu, stdout) < 0)
861 >                        syserror(progname);
862   }
863  
864  
865   writedistance(fname)                    /* write out z file */
866   char    *fname;
867   {
634        extern double   sqrt();
868          int     donorm = normdist && ourview.type == VT_PER;
869          int     fd;
870          int     y;
871          float   *zout;
872  
873 <        if ((fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1) {
874 <                perror(fname);
642 <                exit(1);
643 <        }
873 >        if ((fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1)
874 >                syserror(fname);
875          if (donorm
876          && (zout = (float *)malloc(hresolu*sizeof(float))) == NULL)
877 <                syserror();
877 >                syserror(progname);
878          for (y = vresolu-1; y >= 0; y--) {
879                  if (donorm) {
880                          double  vx, yzn2;
# Line 658 | Line 889 | char   *fname;
889                  } else
890                          zout = zscan(y);
891                  if (write(fd, (char *)zout, hresolu*sizeof(float))
892 <                                < hresolu*sizeof(float)) {
893 <                        perror(fname);
663 <                        exit(1);
664 <                }
892 >                                < hresolu*sizeof(float))
893 >                        syserror(fname);
894          }
895          if (donorm)
896                  free((char *)zout);
# Line 669 | Line 898 | char   *fname;
898   }
899  
900  
672 isfloat(s)                              /* see if string is floating number */
673 register char   *s;
674 {
675        for ( ; *s; s++)
676                if ((*s < '0' || *s > '9') && *s != '.' && *s != '-'
677                                && *s != 'e' && *s != 'E' && *s != '+')
678                        return(0);
679        return(1);
680 }
681
682
901   backfill(x, y)                          /* fill pixel with background */
902   int     x, y;
903   {
904 <        register BYTE   *dest = pscan(y)[x];
905 <
906 <        copycolr(dest, backcolr);
904 >        if (averaging) {
905 >                copycolor(sscan(y)[x], backcolor);
906 >                wscan(y)[x] = 1;
907 >        } else
908 >                copycolr(pscan(y)[x], backcolr);
909          zscan(y)[x] = backz;
910   }
911  
912  
913 < calstart(prog, args)                    /* start fill calculation */
913 > calstart(prog, args)                    /* start fill calculation */
914   char    *prog, *args;
915   {
916          char    combuf[512];
917 <        int     p0[2], p1[2];
917 >        char    *argv[64];
918 >        int     rval;
919 >        register char   **wp, *cp;
920  
921          if (childpid != -1) {
922                  fprintf(stderr, "%s: too many calculations\n", progname);
923                  exit(1);
924          }
925 <        sprintf(combuf, prog, args);
926 <        if (pipe(p0) < 0 || pipe(p1) < 0)
927 <                syserror();
928 <        if ((childpid = vfork()) == 0) {        /* fork calculation */
929 <                close(p0[1]);
930 <                close(p1[0]);
931 <                if (p0[0] != 0) {
932 <                        dup2(p0[0], 0);
933 <                        close(p0[0]);
934 <                }
935 <                if (p1[1] != 1) {
936 <                        dup2(p1[1], 1);
715 <                        close(p1[1]);
716 <                }
717 <                execl("/bin/sh", "sh", "-c", combuf, 0);
718 <                perror("/bin/sh");
719 <                _exit(127);
925 >        strcpy(combuf, prog);
926 >        strcat(combuf, args);
927 >        cp = combuf;
928 >        wp = argv;
929 >        for ( ; ; ) {
930 >                while (isspace(*cp))    /* nullify spaces */
931 >                        *cp++ = '\0';
932 >                if (!*cp)               /* all done? */
933 >                        break;
934 >                *wp++ = cp;             /* add argument to list */
935 >                while (*++cp && !isspace(*cp))
936 >                        ;
937          }
938 <        if (childpid == -1)
939 <                syserror();
940 <        close(p0[0]);
941 <        close(p1[1]);
942 <        if ((psend = fdopen(p0[1], "w")) == NULL)
943 <                syserror();
944 <        if ((precv = fdopen(p1[0], "r")) == NULL)
945 <                syserror();
938 >        *wp = NULL;
939 >                                                /* start process */
940 >        if ((rval = open_process(PDesc, argv)) < 0)
941 >                syserror(progname);
942 >        if (rval == 0) {
943 >                fprintf(stderr, "%s: command not found\n", argv[0]);
944 >                exit(1);
945 >        }
946 >        packsiz = rval/(6*sizeof(float)) - 1;
947 >        if (packsiz > PACKSIZ)
948 >                packsiz = PACKSIZ;
949          queuesiz = 0;
950   }
951  
952  
953 < caldone()                               /* done with calculation */
953 > caldone()                               /* done with calculation */
954   {
735        int     pid;
736
955          if (childpid == -1)
956                  return;
957          clearqueue();
958 <        fclose(psend);
741 <        fclose(precv);
742 <        while ((pid = wait(0)) != -1 && pid != childpid)
743 <                ;
958 >        close_process(PDesc);
959          childpid = -1;
960   }
961  
# Line 748 | Line 963 | caldone()                              /* done with calculation */
963   rcalfill(x, y)                          /* fill with ray-calculated pixel */
964   int     x, y;
965   {
966 <        if (queuesiz >= PACKSIZ)        /* flush queue if needed */
966 >        if (queuesiz >= packsiz)        /* flush queue if needed */
967                  clearqueue();
968                                          /* add position to queue */
969          queue[queuesiz][0] = x;
# Line 760 | Line 975 | int    x, y;
975   clearqueue()                            /* process queue */
976   {
977          FVECT   orig, dir;
978 <        float   fbuf[6];
978 >        float   fbuf[6*(PACKSIZ+1)];
979 >        register float  *fbp;
980          register int    i;
981  
982 +        if (queuesiz == 0)
983 +                return;
984 +        fbp = fbuf;
985          for (i = 0; i < queuesiz; i++) {
986                  viewray(orig, dir, &ourview,
987                                  (queue[i][0]+.5)/hresolu,
988                                  (queue[i][1]+.5)/vresolu);
989 <                fbuf[0] = orig[0]; fbuf[1] = orig[1]; fbuf[2] = orig[2];
990 <                fbuf[3] = dir[0]; fbuf[4] = dir[1]; fbuf[5] = dir[2];
772 <                fwrite((char *)fbuf, sizeof(float), 6, psend);
989 >                *fbp++ = orig[0]; *fbp++ = orig[1]; *fbp++ = orig[2];
990 >                *fbp++ = dir[0]; *fbp++ = dir[1]; *fbp++ = dir[2];
991          }
992 <                                        /* flush output and get results */
993 <        fbuf[3] = fbuf[4] = fbuf[5] = 0.0;              /* mark */
994 <        fwrite((char *)fbuf, sizeof(float), 6, psend);
995 <        if (fflush(psend) == EOF)
996 <                syserror();
992 >                                        /* mark end and get results */
993 >        bzero((char *)fbp, 6*sizeof(float));
994 >        if (process(PDesc, fbuf, fbuf, 4*sizeof(float)*queuesiz,
995 >                        6*sizeof(float)*(queuesiz+1)) !=
996 >                        4*sizeof(float)*queuesiz) {
997 >                fprintf(stderr, "%s: error reading from rtrace process\n",
998 >                                progname);
999 >                exit(1);
1000 >        }
1001 >        fbp = fbuf;
1002          for (i = 0; i < queuesiz; i++) {
780                if (fread((char *)fbuf, sizeof(float), 4, precv) < 4) {
781                        fprintf(stderr, "%s: read error in clearqueue\n",
782                                        progname);
783                        exit(1);
784                }
1003                  if (ourexp > 0 && ourexp != 1.0) {
1004 <                        fbuf[0] *= ourexp;
1005 <                        fbuf[1] *= ourexp;
1006 <                        fbuf[2] *= ourexp;
1004 >                        fbp[0] *= ourexp;
1005 >                        fbp[1] *= ourexp;
1006 >                        fbp[2] *= ourexp;
1007                  }
1008 <                setcolr(pscan(queue[i][1])[queue[i][0]],
1009 <                                fbuf[0], fbuf[1], fbuf[2]);
1010 <                zscan(queue[i][1])[queue[i][0]] = fbuf[3];
1008 >                if (averaging) {
1009 >                        setcolor(sscan(queue[i][1])[queue[i][0]],
1010 >                                        fbp[0], fbp[1], fbp[2]);
1011 >                        wscan(queue[i][1])[queue[i][0]] = 1;
1012 >                } else
1013 >                        setcolr(pscan(queue[i][1])[queue[i][0]],
1014 >                                        fbp[0], fbp[1], fbp[2]);
1015 >                zscan(queue[i][1])[queue[i][0]] = fbp[3];
1016 >                fbp += 4;
1017          }
1018          queuesiz = 0;
1019   }
1020  
1021  
1022 < syserror()                      /* report error and exit */
1022 > syserror(s)                     /* report error and exit */
1023 > char    *s;
1024   {
1025 <        perror(progname);
1025 >        perror(s);
1026          exit(1);
1027   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines