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 2.9 by greg, Fri Oct 2 16:24:22 1992 UTC vs.
Revision 2.21 by greg, Mon Dec 26 21:02:31 1994 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1991 Regents of the University of California */
1 > /* Copyright (c) 1994 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 20 | Line 20 | static char SCCSid[] = "$SunId$ LBL";
20  
21   #include "resolu.h"
22  
23 < #ifndef BSD
24 < #define vfork           fork
25 < #endif
23 > #define LOG2            0.69314718055994530942
24  
25   #define pscan(y)        (ourpict+(y)*hresolu)
26 + #define sscan(y)        (ourspict+(y)*hresolu)
27 + #define wscan(y)        (ourweigh+(y)*hresolu)
28   #define zscan(y)        (ourzbuf+(y)*hresolu)
29 + #define averaging       (ourweigh != NULL)
30  
31 + #define MAXWT           1000.           /* maximum pixel weight (averaging) */
32 +
33   #define F_FORE          1               /* fill foreground */
34   #define F_BACK          2               /* fill background */
35  
# Line 38 | Line 41 | static char SCCSid[] = "$SunId$ LBL";
41  
42   struct position {int x,y; float z;};
43  
44 + #define NSTEPS          64              /* number steps in overlap prescan */
45 + #define MINSTEP         4               /* minimum worthwhile preview step */
46 +
47 + struct bound {int min,max;};
48 +
49   VIEW    ourview = STDVIEW;              /* desired view */
50   int     hresolu = 512;                  /* horizontal resolution */
51   int     vresolu = 512;                  /* vertical resolution */
# Line 45 | Line 53 | double pixaspect = 1.0;                /* pixel aspect ratio */
53  
54   double  zeps = .02;                     /* allowed z epsilon */
55  
56 < COLR    *ourpict;                       /* output picture */
56 > COLR    *ourpict;                       /* output picture (COLR's) */
57 > COLOR   *ourspict;                      /* output pixel sums (averaging) */
58 > float   *ourweigh = NULL;               /* output pixel weights (averaging) */
59   float   *ourzbuf;                       /* corresponding z-buffer */
60  
61   char    *progname;
# Line 55 | Line 65 | int    fillsamp = 0;                   /* sample separation (0 == inf) */
65   extern int      backfill(), rcalfill(); /* fill functions */
66   int     (*fillfunc)() = backfill;       /* selected fill function */
67   COLR    backcolr = BLKCOLR;             /* background color */
68 + COLOR   backcolor = BLKCOLOR;           /* background color (float) */
69   double  backz = 0.0;                    /* background z value */
70   int     normdist = 1;                   /* normalized distance? */
71 < double  ourexp = -1;                    /* output picture exposure */
71 > double  ourexp = -1;                    /* original picture exposure */
72 > int     expadj = 0;                     /* exposure adjustment (f-stops) */
73 > double  rexpadj = 1;                    /* real exposure adjustment */
74  
75   VIEW    theirview = STDVIEW;            /* input view */
76   int     gotview;                        /* got input view? */
77   int     wrongformat = 0;                /* input in another format? */
78   RESOLU  tresolu;                        /* input resolution */
79   double  theirexp;                       /* input picture exposure */
80 < double  theirs2ours[4][4];              /* transformation matrix */
80 > MAT4    theirs2ours;                    /* transformation matrix */
81   int     hasmatrix = 0;                  /* has transformation matrix */
82  
83   int     PDesc[3] = {-1,-1,-1};          /* rtrace process descriptor */
# Line 73 | Line 86 | unsigned short queue[PACKSIZ][2];      /* pending pixels */
86   int     packsiz;                        /* actual packet size */
87   int     queuesiz;                       /* number of pixels pending */
88  
89 + extern double   movepixel();
90  
91 +
92   main(argc, argv)                        /* interpolate pictures */
93   int     argc;
94   char    *argv[];
# Line 82 | Line 97 | char   *argv[];
97                                  badarg(argc-i-1,argv+i+1,al)) \
98                                  goto badopt
99          int     gotvfile = 0;
100 +        int     doavg = -1;
101          char    *zfile = NULL;
102 +        char    *expcomp = NULL;
103          char    *err;
104          int     i, rval;
105  
# Line 95 | Line 112 | char   *argv[];
112                          continue;
113                  }
114                  switch (argv[i][1]) {
115 +                case 'e':                               /* exposure */
116 +                        check(2,"f");
117 +                        expcomp = argv[++i];
118 +                        break;
119                  case 't':                               /* threshold */
120                          check(2,"f");
121                          zeps = atof(argv[++i]);
122                          break;
123 +                case 'a':                               /* average */
124 +                        check(2,NULL);
125 +                        doavg = 1;
126 +                        break;
127 +                case 'q':                               /* quick (no avg.) */
128 +                        check(2,NULL);
129 +                        doavg = 0;
130 +                        break;
131                  case 'n':                               /* dist. normalized? */
132                          check(2,NULL);
133                          normdist = !normdist;
# Line 128 | Line 157 | char   *argv[];
157                          case 'c':                               /* color */
158                                  check(3,"fff");
159                                  fillfunc = backfill;
160 <                                setcolr(backcolr, atof(argv[i+1]),
160 >                                setcolor(backcolor, atof(argv[i+1]),
161                                          atof(argv[i+2]), atof(argv[i+3]));
162 +                                setcolr(backcolr, colval(backcolor,RED),
163 +                                                colval(backcolor,GRN),
164 +                                                colval(backcolor,BLU));
165                                  i += 3;
166                                  break;
167                          case 'z':                               /* z value */
# Line 169 | Line 201 | char   *argv[];
201                                  goto badopt;
202                          check(3,"s");
203                          gotvfile = viewfile(argv[++i], &ourview, 0, 0);
204 <                        if (gotvfile < 0) {
205 <                                perror(argv[i]);
206 <                                exit(1);
175 <                        } else if (gotvfile == 0) {
204 >                        if (gotvfile < 0)
205 >                                syserror(argv[i]);
206 >                        else if (gotvfile == 0) {
207                                  fprintf(stderr, "%s: bad view file\n",
208                                                  argv[i]);
209                                  exit(1);
# Line 190 | Line 221 | char   *argv[];
221                  goto userr;
222          if (fillsamp == 1)
223                  fillo &= ~F_BACK;
224 +        if (doavg < 0)
225 +                doavg = (argc-i) > 2;
226 +        if (expcomp != NULL)
227 +                if (expcomp[0] == '+' | expcomp[0] == '-') {
228 +                        expadj = atof(expcomp) + (expcomp[0]=='+' ? .5 : -.5);
229 +                        if (doavg)
230 +                                rexpadj = pow(2.0, atof(expcomp));
231 +                        else
232 +                                rexpadj = pow(2.0, (double)expadj);
233 +                } else {
234 +                        if (!isflt(expcomp))
235 +                                goto userr;
236 +                        rexpadj = atof(expcomp);
237 +                        expadj = log(rexpadj)/LOG2 + (rexpadj>1 ? .5 : -.5);
238 +                        if (!doavg)
239 +                                rexpadj = pow(2.0, (double)expadj);
240 +                }
241                                                  /* set view */
242 <        if (err = setview(&ourview)) {
242 >        if ((err = setview(&ourview)) != NULL) {
243                  fprintf(stderr, "%s: %s\n", progname, err);
244                  exit(1);
245          }
246          normaspect(viewaspect(&ourview), &pixaspect, &hresolu, &vresolu);
247                                                  /* allocate frame */
248 <        ourpict = (COLR *)bmalloc(hresolu*vresolu*sizeof(COLR));
248 >        if (doavg) {
249 >                ourspict = (COLOR *)bmalloc(hresolu*vresolu*sizeof(COLOR));
250 >                ourweigh = (float *)bmalloc(hresolu*vresolu*sizeof(float));
251 >                if (ourspict == NULL | ourweigh == NULL)
252 >                        syserror(progname);
253 >        } else {
254 >                ourpict = (COLR *)bmalloc(hresolu*vresolu*sizeof(COLR));
255 >                if (ourpict == NULL)
256 >                        syserror(progname);
257 >        }
258          ourzbuf = (float *)bmalloc(hresolu*vresolu*sizeof(float));
259 <        if (ourpict == NULL || ourzbuf == NULL)
260 <                syserror();
259 >        if (ourzbuf == NULL)
260 >                syserror(progname);
261          bzero((char *)ourzbuf, hresolu*vresolu*sizeof(float));
262 +                                                        /* new header */
263 +        newheader("RADIANCE", stdout);
264                                                          /* get input */
265          for ( ; i < argc; i += 2)
266                  addpicture(argv[i], argv[i+1]);
# Line 212 | Line 271 | char   *argv[];
271                  fillpicture(fillfunc);
272                                                          /* close calculation */
273          caldone();
274 +                                                        /* aft clipping */
275 +        clipaft();
276                                                          /* add to header */
277          printargs(argc, argv, stdout);
278          if (gotvfile) {
# Line 219 | Line 280 | char   *argv[];
280                  fprintview(&ourview, stdout);
281                  putc('\n', stdout);
282          }
283 <        if (pixaspect < .99 || pixaspect > 1.01)
283 >        if (pixaspect < .99 | pixaspect > 1.01)
284                  fputaspect(pixaspect, stdout);
285 <        if (ourexp > 0 && (ourexp < .995 || ourexp > 1.005))
285 >        if (ourexp > 0)
286 >                ourexp *= rexpadj;
287 >        else
288 >                ourexp = rexpadj;
289 >        if (ourexp < .995 | ourexp > 1.005)
290                  fputexpos(ourexp, stdout);
291          fputformat(COLRFMT, stdout);
292          putc('\n', stdout);
# Line 234 | Line 299 | char   *argv[];
299          exit(0);
300   userr:
301          fprintf(stderr,
302 <        "Usage: %s [view opts][-t eps][-z zout][-fT][-n] pfile zspec ..\n",
302 >        "Usage: %s [view opts][-t eps][-z zout][-e spec][-a|-q][-fT][-n] pfile zspec ..\n",
303                          progname);
304          exit(1);
305   #undef check
# Line 246 | Line 311 | char   *s;
311   {
312          char    fmt[32];
313  
314 <        if (isformat(s)) {
315 <                formatval(fmt, s);
314 >        if (isheadid(s))
315 >                return;
316 >        if (formatval(fmt, s)) {
317                  wrongformat = strcmp(fmt, COLRFMT);
318                  return;
319          }
# Line 272 | Line 338 | char   *pfile, *zspec;
338          COLR    *scanin;
339          float   *zin;
340          struct position *plast;
341 +        struct bound    *xlim, ylim;
342          int     y;
343                                          /* open picture file */
344 <        if ((pfp = fopen(pfile, "r")) == NULL) {
345 <                perror(pfile);
279 <                exit(1);
280 <        }
344 >        if ((pfp = fopen(pfile, "r")) == NULL)
345 >                syserror(pfile);
346                                          /* get header with exposure and view */
347          theirexp = 1.0;
348          gotview = 0;
# Line 297 | Line 362 | char   *pfile, *zspec;
362          }
363                                          /* compute transformation */
364          hasmatrix = pixform(theirs2ours, &theirview, &ourview);
300                                        /* allocate scanlines */
301        scanin = (COLR *)malloc(scanlen(&tresolu)*sizeof(COLR));
302        zin = (float *)malloc(scanlen(&tresolu)*sizeof(float));
303        plast = (struct position *)calloc(scanlen(&tresolu),
304                        sizeof(struct position));
305        if (scanin == NULL || zin == NULL || plast == NULL)
306                syserror();
365                                          /* get z specification or file */
366 +        zin = (float *)malloc(scanlen(&tresolu)*sizeof(float));
367 +        if (zin == NULL)
368 +                syserror(progname);
369          if ((zfd = open(zspec, O_RDONLY)) == -1) {
370                  double  zvalue;
371                  register int    x;
372 <                if (!isfloat(zspec) || (zvalue = atof(zspec)) <= 0.0) {
373 <                        perror(zspec);
313 <                        exit(1);
314 <                }
372 >                if (!isflt(zspec) || (zvalue = atof(zspec)) <= 0.0)
373 >                        syserror(zspec);
374                  for (x = scanlen(&tresolu); x-- > 0; )
375                          zin[x] = zvalue;
376          }
377 <                                        /* load image */
378 <        for (y = 0; y < numscans(&tresolu); y++) {
377 >                                        /* compute transferrable perimeter */
378 >        xlim = (struct bound *)malloc(numscans(&tresolu)*sizeof(struct bound));
379 >        if (xlim == NULL)
380 >                syserror(progname);
381 >        if (!getperim(xlim, &ylim, zin, zfd)) { /* overlapping area? */
382 >                free((char *)zin);
383 >                free((char *)xlim);
384 >                if (zfd != -1)
385 >                        close(zfd);
386 >                fclose(pfp);
387 >                return;
388 >        }
389 >                                        /* allocate scanlines */
390 >        scanin = (COLR *)malloc(scanlen(&tresolu)*sizeof(COLR));
391 >        plast = (struct position *)calloc(scanlen(&tresolu),
392 >                        sizeof(struct position));
393 >        if (scanin == NULL | plast == NULL)
394 >                syserror(progname);
395 >                                        /* skip to starting point */
396 >        for (y = 0; y < ylim.min; y++)
397                  if (freadcolrs(scanin, scanlen(&tresolu), pfp) < 0) {
398                          fprintf(stderr, "%s: read error\n", pfile);
399                          exit(1);
400                  }
401 <                if (zfd != -1 && read(zfd,(char *)zin,
402 <                                scanlen(&tresolu)*sizeof(float))
403 <                                < scanlen(&tresolu)*sizeof(float)) {
404 <                        fprintf(stderr, "%s: read error\n", zspec);
401 >        if (zfd != -1 && lseek(zfd,
402 >                        (long)ylim.min*scanlen(&tresolu)*sizeof(float), 0) < 0)
403 >                syserror(zspec);
404 >                                        /* load image */
405 >        for (y = ylim.min; y <= ylim.max; y++) {
406 >                if (freadcolrs(scanin, scanlen(&tresolu), pfp) < 0) {
407 >                        fprintf(stderr, "%s: read error\n", pfile);
408                          exit(1);
409                  }
410 <                addscanline(y, scanin, zin, plast);
410 >                if (zfd != -1 && read(zfd, (char *)zin,
411 >                                scanlen(&tresolu)*sizeof(float))
412 >                                < scanlen(&tresolu)*sizeof(float))
413 >                        syserror(zspec);
414 >                addscanline(xlim+y, y, scanin, zin, plast);
415          }
416                                          /* clean up */
417 +        free((char *)xlim);
418          free((char *)scanin);
419          free((char *)zin);
420          free((char *)plast);
# Line 340 | Line 425 | char   *pfile, *zspec;
425  
426  
427   pixform(xfmat, vw1, vw2)                /* compute view1 to view2 matrix */
428 < register double xfmat[4][4];
428 > register MAT4   xfmat;
429   register VIEW   *vw1, *vw2;
430   {
431          double  m4t[4][4];
# Line 380 | Line 465 | register VIEW  *vw1, *vw2;
465   }
466  
467  
468 < addscanline(y, pline, zline, lasty)     /* add scanline to output */
468 > addscanline(xl, y, pline, zline, lasty) /* add scanline to output */
469 > struct bound    *xl;
470   int     y;
471   COLR    *pline;
472   float   *zline;
# Line 388 | Line 474 | struct position        *lasty;         /* input/output */
474   {
475          FVECT   pos;
476          struct position lastx, newpos;
477 +        double  wt;
478          register int    x;
479  
480          lastx.z = 0;
481 <        for (x = scanlen(&tresolu); x-- > 0; ) {
481 >        for (x = xl->max; x >= xl->min; x--) {
482                  pix2loc(pos, &tresolu, x, y);
483                  pos[2] = zline[x];
484 <                if (movepixel(pos) < 0) {
484 >                if ((wt = movepixel(pos)) <= FTINY) {
485                          lasty[x].z = lastx.z = 0;       /* mark invalid */
486                          continue;
487                  }
488 +                                        /* add pixel to our image */
489                  newpos.x = pos[0] * hresolu;
490                  newpos.y = pos[1] * vresolu;
491                  newpos.z = zline[x];
492 <                                        /* add pixel to our image */
493 <                if (pos[0] >= 0 && newpos.x < hresolu
494 <                                && pos[1] >= 0 && newpos.y < vresolu) {
495 <                        addpixel(&newpos, &lastx, &lasty[x], pline[x], pos[2]);
408 <                        lasty[x].x = lastx.x = newpos.x;
409 <                        lasty[x].y = lastx.y = newpos.y;
410 <                        lasty[x].z = lastx.z = newpos.z;
411 <                } else
412 <                        lasty[x].z = lastx.z = 0;       /* mark invalid */
492 >                addpixel(&newpos, &lastx, &lasty[x], pline[x], wt, pos[2]);
493 >                lasty[x].x = lastx.x = newpos.x;
494 >                lasty[x].y = lastx.y = newpos.y;
495 >                lasty[x].z = lastx.z = newpos.z;
496          }
497   }
498  
499  
500 < addpixel(p0, p1, p2, pix, z)            /* fill in pixel parallelogram */
500 > addpixel(p0, p1, p2, pix, w, z)         /* fill in pixel parallelogram */
501   struct position *p0, *p1, *p2;
502   COLR    pix;
503 + double  w;
504   double  z;
505   {
506          double  zt = 2.*zeps*p0->z;             /* threshold */
507 +        COLOR   pval;                           /* converted+weighted pixel */
508          int     s1x, s1y, s2x, s2y;             /* step sizes */
509          int     l1, l2, c1, c2;                 /* side lengths and counters */
510          int     p1isy;                          /* p0p1 along y? */
# Line 433 | Line 518 | double z;
518                  l1 = ABS(s1x);
519                  if (p1isy = (ABS(s1y) > l1))
520                          l1 = ABS(s1y);
521 +                else if (l1 < 1)
522 +                        l1 = 1;
523          } else {
524                  l1 = s1x = s1y = 1;
525                  p1isy = -1;
# Line 448 | Line 535 | double z;
535                          if (p1isy != 0 && ABS(s2x) > l2)
536                                  l2 = ABS(s2x);
537                  }
538 +                if (l2 < 1)
539 +                        l2 = 1;
540          } else
541                  l2 = s2x = s2y = 1;
542                                          /* fill the parallelogram */
543 +        if (averaging) {
544 +                colr_color(pval, pix);
545 +                scalecolor(pval, w);
546 +        }
547          for (c1 = l1; c1-- > 0; ) {
548                  x1 = p0->x + c1*s1x/l1;
549                  y1 = p0->y + c1*s1y/l1;
# Line 461 | Line 554 | double z;
554                          y = y1 + c2*s2y/l2;
555                          if (y < 0 || y >= vresolu)
556                                  continue;
557 <                        if (zscan(y)[x] <= 0 || zscan(y)[x]-z
557 >                        if (averaging) {
558 >                                if (zscan(y)[x] <= 0 || zscan(y)[x]-z
559                                                  > zeps*zscan(y)[x]) {
560 <                                zscan(y)[x] = z;
560 >                                        copycolor(sscan(y)[x], pval);
561 >                                        wscan(y)[x] = w;
562 >                                        zscan(y)[x] = z;
563 >                                } else if (z-zscan(y)[x] <= zeps*zscan(y)[x]) {
564 >                                        addcolor(sscan(y)[x], pval);
565 >                                        wscan(y)[x] += w;
566 >                                }
567 >                        } else if (zscan(y)[x] <= 0 || zscan(y)[x]-z
568 >                                                > zeps*zscan(y)[x]) {
569                                  copycolr(pscan(y)[x], pix);
570 +                                zscan(y)[x] = z;
571                          }
572                  }
573          }
574   }
575  
576  
577 + double
578   movepixel(pos)                          /* reposition image point */
579 < FVECT   pos;
579 > register FVECT  pos;
580   {
581 <        FVECT   pt, direc;
581 >        FVECT   pt, tdir, odir;
582 >        double  d;
583 >        register int    i;
584          
585          if (pos[2] <= 0)                /* empty pixel */
586 <                return(-1);
587 <        if (hasmatrix) {
586 >                return(0);
587 >        if (!averaging && hasmatrix) {
588                  pos[0] += theirview.hoff - .5;
589                  pos[1] += theirview.voff - .5;
590                  if (theirview.type == VT_PER) {
591 <                        if (normdist)   /* adjust for eye-ray distance */
592 <                                pos[2] /= sqrt( 1.
593 <                                        + pos[0]*pos[0]*theirview.hn2
488 <                                        + pos[1]*pos[1]*theirview.vn2 );
591 >                        if (normdist)                   /* adjust distance */
592 >                                pos[2] /= sqrt(1. + pos[0]*pos[0]*theirview.hn2
593 >                                                + pos[1]*pos[1]*theirview.vn2);
594                          pos[0] *= pos[2];
595                          pos[1] *= pos[2];
596                  }
597                  multp3(pos, pos, theirs2ours);
598                  if (pos[2] <= 0)
599 <                        return(-1);
599 >                        return(0);
600                  if (ourview.type == VT_PER) {
601                          pos[0] /= pos[2];
602                          pos[1] /= pos[2];
603                  }
604                  pos[0] += .5 - ourview.hoff;
605                  pos[1] += .5 - ourview.voff;
606 +        } else {
607 +                if (viewray(pt, tdir, &theirview, pos[0], pos[1]) < -FTINY)
608 +                        return(0);
609 +                if (!normdist && theirview.type == VT_PER)      /* adjust */
610 +                        pos[2] *= sqrt(1. + pos[0]*pos[0]*theirview.hn2
611 +                                        + pos[1]*pos[1]*theirview.vn2);
612 +                pt[0] += tdir[0]*pos[2];
613 +                pt[1] += tdir[1]*pos[2];
614 +                pt[2] += tdir[2]*pos[2];
615 +                viewloc(pos, &ourview, pt);
616 +                if (pos[2] <= 0)
617 +                        return(0);
618 +        }
619 +        if (pos[0] < 0 || pos[0] >= 1-FTINY || pos[1] < 0 || pos[1] >= 1-FTINY)
620                  return(0);
621 +        if (!averaging)
622 +                return(1);
623 +        if (ourview.type == VT_PAR)             /* compute our direction */
624 +                VCOPY(odir, ourview.vdir);
625 +        else
626 +                for (i = 0; i < 3; i++)
627 +                        odir[i] = (pt[i] - ourview.vp[i])/pos[2];
628 +        d = DOT(odir,tdir);                     /* compute pixel weight */
629 +        if (d >= 1.-1./MAXWT/MAXWT)
630 +                return(MAXWT);          /* clip to maximum weight */
631 +        return(1./sqrt(1.-d));
632 + }
633 +
634 +
635 + getperim(xl, yl, zline, zfd)            /* compute overlapping image area */
636 + register struct bound   *xl;
637 + struct bound    *yl;
638 + float   *zline;
639 + int     zfd;
640 + {
641 +        int     step;
642 +        FVECT   pos;
643 +        register int    x, y;
644 +                                                /* set up step size */
645 +        if (scanlen(&tresolu) < numscans(&tresolu))
646 +                step = scanlen(&tresolu)/NSTEPS;
647 +        else
648 +                step = numscans(&tresolu)/NSTEPS;
649 +        if (step < MINSTEP) {                   /* not worth cropping? */
650 +                yl->min = 0;
651 +                yl->max = numscans(&tresolu) - 1;
652 +                x = scanlen(&tresolu) - 1;
653 +                for (y = numscans(&tresolu); y--; ) {
654 +                        xl[y].min = 0;
655 +                        xl[y].max = x;
656 +                }
657 +                return(1);
658          }
659 <        if (viewray(pt, direc, &theirview, pos[0], pos[1]) < 0)
660 <                return(-1);
661 <        pt[0] += direc[0]*pos[2];
662 <        pt[1] += direc[1]*pos[2];
663 <        pt[2] += direc[2]*pos[2];
664 <        viewloc(pos, &ourview, pt);
665 <        if (pos[2] <= 0)
666 <                return(-1);
667 <        return(0);
659 >        yl->min = 32000; yl->max = 0;           /* search for points on image */
660 >        for (y = step - 1; y < numscans(&tresolu); y += step) {
661 >                if (zfd != -1) {
662 >                        if (lseek(zfd, (long)y*scanlen(&tresolu)*sizeof(float),
663 >                                        0) < 0)
664 >                                syserror("lseek");
665 >                        if (read(zfd, (char *)zline,
666 >                                        scanlen(&tresolu)*sizeof(float))
667 >                                        < scanlen(&tresolu)*sizeof(float))
668 >                                syserror("read");
669 >                }
670 >                xl[y].min = 32000; xl[y].max = 0;               /* x max */
671 >                for (x = scanlen(&tresolu); (x -= step) > 0; ) {
672 >                        pix2loc(pos, &tresolu, x, y);
673 >                        pos[2] = zline[x];
674 >                        if (movepixel(pos) > FTINY) {
675 >                                xl[y].max = x + step - 1;
676 >                                xl[y].min = x - step + 1;       /* x min */
677 >                                if (xl[y].min < 0)
678 >                                        xl[y].min = 0;
679 >                                for (x = step - 1; x < xl[y].max; x += step) {
680 >                                        pix2loc(pos, &tresolu, x, y);
681 >                                        pos[2] = zline[x];
682 >                                        if (movepixel(pos) > FTINY) {
683 >                                                xl[y].min = x - step + 1;
684 >                                                break;
685 >                                        }
686 >                                }
687 >                                if (y < yl->min)                /* y limits */
688 >                                        yl->min = y - step + 1;
689 >                                yl->max = y + step - 1;
690 >                                break;
691 >                        }
692 >                }
693 >                                                        /* fill in between */
694 >                if (y < step) {
695 >                        xl[y-1].min = xl[y].min;
696 >                        xl[y-1].max = xl[y].max;
697 >                } else {
698 >                        if (xl[y].min < xl[y-step].min)
699 >                                xl[y-1].min = xl[y].min;
700 >                        else
701 >                                xl[y-1].min = xl[y-step].min;
702 >                        if (xl[y].max > xl[y-step].max)
703 >                                xl[y-1].max = xl[y].max;
704 >                        else
705 >                                xl[y-1].max = xl[y-step].max;
706 >                }
707 >                for (x = 2; x < step; x++)
708 >                        copystruct(xl+y-x, xl+y-1);
709 >        }
710 >        if (yl->max >= numscans(&tresolu))
711 >                yl->max = numscans(&tresolu) - 1;
712 >        y -= step;
713 >        for (x = numscans(&tresolu) - 1; x > y; x--)    /* fill bottom rows */
714 >                copystruct(xl+x, xl+y);
715 >        return(yl->max >= yl->min);
716   }
717  
718  
# Line 522 | Line 726 | int    samp;
726                                                          /* get back buffer */
727          yback = (int *)malloc(hresolu*sizeof(int));
728          if (yback == NULL)
729 <                syserror();
729 >                syserror(progname);
730          for (x = 0; x < hresolu; x++)
731                  yback[x] = -2;
732          /*
# Line 582 | Line 786 | int    samp;
786                                                  < zscan(y)[xback] ) ) {
787                                          if (samp > 0 && ABS(x-xback) >= samp)
788                                                  goto fillit;
789 <                                        copycolr(pscan(y)[x],pscan(y)[xback]);
789 >                                        if (averaging) {
790 >                                                copycolor(sscan(y)[x],
791 >                                                        sscan(y)[xback]);
792 >                                                wscan(y)[x] = wscan(y)[xback];
793 >                                        } else
794 >                                                copycolr(pscan(y)[x],
795 >                                                        pscan(y)[xback]);
796                                          zscan(y)[x] = zscan(y)[xback];
797                                  } else {
798                                          if (samp > 0 && ABS(y-yback[x]) > samp)
799                                                  goto fillit;
800 <                                        copycolr(pscan(y)[x],pscan(yback[x])[x]);
800 >                                        if (averaging) {
801 >                                                copycolor(sscan(y)[x],
802 >                                                        sscan(yback[x])[x]);
803 >                                                wscan(y)[x] =
804 >                                                        wscan(yback[x])[x];
805 >                                        } else
806 >                                                copycolr(pscan(y)[x],
807 >                                                        pscan(yback[x])[x]);
808                                          zscan(y)[x] = zscan(yback[x])[x];
809                                  }
810                                  continue;
# Line 619 | Line 836 | int    (*fill)();
836   }
837  
838  
839 < writepicture()                          /* write out picture */
839 > clipaft()                       /* perform aft clipping as indicated */
840   {
841 +        register int    x, y;
842 +        double  tstdist;
843 +        double  yzn2, vx;
844 +
845 +        if (ourview.vaft <= FTINY)
846 +                return;
847 +        tstdist = ourview.vaft - ourview.vfore;
848 +        for (y = 0; y < vresolu; y++) {
849 +                if (ourview.type == VT_PER) {           /* adjust distance */
850 +                        yzn2 = (y+.5)/vresolu + ourview.voff - .5;
851 +                        yzn2 = 1. + yzn2*yzn2*ourview.vn2;
852 +                        tstdist = (ourview.vaft - ourview.vfore)*sqrt(yzn2);
853 +                }
854 +                for (x = 0; x < hresolu; x++)
855 +                        if (zscan(y)[x] > tstdist) {
856 +                                if (ourview.type == VT_PER) {
857 +                                        vx = (x+.5)/hresolu + ourview.hoff - .5;
858 +                                        if (zscan(y)[x] <= (ourview.vaft -
859 +                                                        ourview.vfore) *
860 +                                                sqrt(vx*vx*ourview.hn2 + yzn2))
861 +                                                continue;
862 +                                }
863 +                                if (averaging)
864 +                                        bzero(sscan(y)[x], sizeof(COLOR));
865 +                                else
866 +                                        bzero(pscan(y)[x], sizeof(COLR));
867 +                                zscan(y)[x] = 0.0;
868 +                        }
869 +        }
870 + }
871 +
872 +
873 + writepicture()                          /* write out picture (alters buffer) */
874 + {
875          int     y;
876 +        register int    x;
877 +        double  d;
878  
879          fprtresolu(hresolu, vresolu, stdout);
880          for (y = vresolu-1; y >= 0; y--)
881 <                if (fwritecolrs(pscan(y), hresolu, stdout) < 0)
882 <                        syserror();
881 >                if (averaging) {
882 >                        for (x = 0; x < hresolu; x++) { /* average pixels */
883 >                                d = rexpadj/wscan(y)[x];
884 >                                scalecolor(sscan(y)[x], d);
885 >                        }
886 >                        if (fwritescan(sscan(y), hresolu, stdout) < 0)
887 >                                syserror(progname);
888 >                } else {
889 >                        if (expadj)
890 >                                shiftcolrs(pscan(y), hresolu, expadj);
891 >                        if (fwritecolrs(pscan(y), hresolu, stdout) < 0)
892 >                                syserror(progname);
893 >                }
894   }
895  
896  
897   writedistance(fname)                    /* write out z file */
898   char    *fname;
899   {
900 <        int     donorm = normdist && ourview.type == VT_PER;
900 >        int     donorm = normdist && ourview.type == VT_PER &&
901 >                        !averaging && hasmatrix;
902          int     fd;
903          int     y;
904          float   *zout;
905  
906 <        if ((fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1) {
907 <                perror(fname);
643 <                exit(1);
644 <        }
906 >        if ((fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1)
907 >                syserror(fname);
908          if (donorm
909          && (zout = (float *)malloc(hresolu*sizeof(float))) == NULL)
910 <                syserror();
910 >                syserror(progname);
911          for (y = vresolu-1; y >= 0; y--) {
912                  if (donorm) {
913                          double  vx, yzn2;
# Line 659 | Line 922 | char   *fname;
922                  } else
923                          zout = zscan(y);
924                  if (write(fd, (char *)zout, hresolu*sizeof(float))
925 <                                < hresolu*sizeof(float)) {
926 <                        perror(fname);
664 <                        exit(1);
665 <                }
925 >                                < hresolu*sizeof(float))
926 >                        syserror(fname);
927          }
928          if (donorm)
929                  free((char *)zout);
# Line 670 | Line 931 | char   *fname;
931   }
932  
933  
673 isfloat(s)                              /* see if string is floating number */
674 register char   *s;
675 {
676        for ( ; *s; s++)
677                if ((*s < '0' || *s > '9') && *s != '.' && *s != '-'
678                                && *s != 'e' && *s != 'E' && *s != '+')
679                        return(0);
680        return(1);
681 }
682
683
934   backfill(x, y)                          /* fill pixel with background */
935   int     x, y;
936   {
937 <        register BYTE   *dest = pscan(y)[x];
938 <
939 <        copycolr(dest, backcolr);
937 >        if (averaging) {
938 >                copycolor(sscan(y)[x], backcolor);
939 >                wscan(y)[x] = 1;
940 >        } else
941 >                copycolr(pscan(y)[x], backcolr);
942          zscan(y)[x] = backz;
943   }
944  
# Line 708 | Line 960 | char   *prog, *args;
960          cp = combuf;
961          wp = argv;
962          for ( ; ; ) {
963 <                while (isspace(*cp)) cp++;
964 <                if (!*cp) break;
965 <                *wp++ = cp;
966 <                while (!isspace(*cp))
967 <                        if (!*cp++) goto done;
968 <                *cp++ = '\0';
963 >                while (isspace(*cp))    /* nullify spaces */
964 >                        *cp++ = '\0';
965 >                if (!*cp)               /* all done? */
966 >                        break;
967 >                *wp++ = cp;             /* add argument to list */
968 >                while (*++cp && !isspace(*cp))
969 >                        ;
970          }
718 done:
971          *wp = NULL;
972                                                  /* start process */
973          if ((rval = open_process(PDesc, argv)) < 0)
974 <                syserror();
974 >                syserror(progname);
975          if (rval == 0) {
976                  fprintf(stderr, "%s: command not found\n", argv[0]);
977                  exit(1);
# Line 760 | Line 1012 | clearqueue()                           /* process queue */
1012          register float  *fbp;
1013          register int    i;
1014  
1015 +        if (queuesiz == 0)
1016 +                return;
1017          fbp = fbuf;
1018          for (i = 0; i < queuesiz; i++) {
1019                  viewray(orig, dir, &ourview,
# Line 784 | Line 1038 | clearqueue()                           /* process queue */
1038                          fbp[1] *= ourexp;
1039                          fbp[2] *= ourexp;
1040                  }
1041 <                setcolr(pscan(queue[i][1])[queue[i][0]],
1042 <                                fbp[0], fbp[1], fbp[2]);
1041 >                if (averaging) {
1042 >                        setcolor(sscan(queue[i][1])[queue[i][0]],
1043 >                                        fbp[0], fbp[1], fbp[2]);
1044 >                        wscan(queue[i][1])[queue[i][0]] = 1;
1045 >                } else
1046 >                        setcolr(pscan(queue[i][1])[queue[i][0]],
1047 >                                        fbp[0], fbp[1], fbp[2]);
1048                  zscan(queue[i][1])[queue[i][0]] = fbp[3];
1049                  fbp += 4;
1050          }
# Line 793 | Line 1052 | clearqueue()                           /* process queue */
1052   }
1053  
1054  
1055 < syserror()                      /* report error and exit */
1055 > syserror(s)                     /* report error and exit */
1056 > char    *s;
1057   {
1058 <        perror(progname);
1058 >        perror(s);
1059          exit(1);
1060   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines