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.18 by greg, Fri Dec 23 19:15:48 1994 UTC vs.
Revision 2.19 by greg, Fri Dec 23 22:35:12 1994 UTC

# Line 21 | Line 21 | static char SCCSid[] = "$SunId$ LBL";
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  
# Line 46 | 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 56 | 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 65 | Line 73 | int    gotview;                        /* got input view? */
73   int     wrongformat = 0;                /* input in another format? */
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     PDesc[3] = {-1,-1,-1};          /* rtrace process descriptor */
# Line 74 | Line 82 | 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[];
# Line 83 | Line 93 | char   *argv[];
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 100 | Line 111 | char   *argv[];
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,NULL);
124                          normdist = !normdist;
# Line 129 | Line 148 | char   *argv[];
148                          case 'c':                               /* color */
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 */
# Line 197 | Line 219 | char   *argv[];
219          }
220          normaspect(viewaspect(&ourview), &pixaspect, &hresolu, &vresolu);
221                                                  /* allocate frame */
222 <        ourpict = (COLR *)bmalloc(hresolu*vresolu*sizeof(COLR));
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 (ourpict == NULL || ourzbuf == NULL)
235 >        if (ourzbuf == NULL)
236                  syserror(progname);
237          bzero((char *)ourzbuf, hresolu*vresolu*sizeof(float));
238                                                          /* new header */
# Line 308 | Line 341 | char   *pfile, *zspec;
341          if ((zfd = open(zspec, O_RDONLY)) == -1) {
342                  double  zvalue;
343                  register int    x;
344 <                if (!isfloat(zspec) || (zvalue = atof(zspec)) <= 0.0)
344 >                if (!isflt(zspec) || (zvalue = atof(zspec)) <= 0.0)
345                          syserror(zspec);
346                  for (x = scanlen(&tresolu); x-- > 0; )
347                          zin[x] = zvalue;
# Line 364 | 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 413 | Line 446 | struct position        *lasty;         /* input/output */
446   {
447          FVECT   pos;
448          struct position lastx, newpos;
449 +        double  wt;
450          register int    x;
451  
452          lastx.z = 0;
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]);
433 <                        lasty[x].x = lastx.x = newpos.x;
434 <                        lasty[x].y = lastx.y = newpos.y;
435 <                        lasty[x].z = lastx.z = newpos.z;
436 <                } else
437 <                        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 458 | 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 473 | 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 486 | 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          double  d0, d1;
554 <        FVECT   pt, direc;
554 >        FVECT   pt, tdir, odir;
555 >        register int    i;
556          
557          if (pos[2] <= 0)                /* empty pixel */
558 <                return(-1);
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 (hasmatrix) {
564 >        if (!averaging && hasmatrix) {
565                  pos[0] += theirview.hoff - .5;
566                  pos[1] += theirview.voff - .5;
567                  if (theirview.type == VT_PER) {
# Line 518 | Line 570 | FVECT  pos;
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 <                return(0);
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 (viewray(pt, direc, &theirview, pos[0], pos[1]) < -FTINY)
591 <                return(-1);
592 <        pt[0] += direc[0]*pos[2];
593 <        pt[1] += direc[1]*pos[2];
594 <        pt[2] += direc[2]*pos[2];
595 <        viewloc(pos, &ourview, pt);
596 <        if (pos[2] <= 0)
597 <                return(-1);
598 <        return(0);
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  
# Line 578 | Line 644 | int    zfd;
644                  for (x = scanlen(&tresolu); (x -= step) > 0; ) {
645                          pix2loc(pos, &tresolu, x, y);
646                          pos[2] = zline[x];
647 <                        if (movepixel(pos) == 0 && pos[0] >= 0 &&
582 <                                        pos[0] < 1 && pos[1] >= 0 &&
583 <                                        pos[1] < 1) {
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)
# Line 588 | Line 652 | int    zfd;
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) == 0 &&
592 <                                                        pos[0] >= 0 &&
593 <                                                        pos[0] < 1 &&
594 <                                                        pos[1] >= 0 &&
595 <                                                        pos[1] < 1) {
655 >                                        if (movepixel(pos) > FTINY) {
656                                                  xl[y].min = x - step + 1;
657                                                  break;
658                                          }
# Line 698 | 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 759 | Line 832 | clipaft()                      /* perform aft clipping as indicated */
832                                                  sqrt(vx*vx*ourview.hn2 + yzn2))
833                                                  continue;
834                                  }
835 <                                bzero(pscan(y)[x], sizeof(COLR));
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          }
# Line 769 | Line 845 | clipaft()                      /* perform aft clipping as indicated */
845   writepicture()                          /* write out picture */
846   {
847          int     y;
848 +        register int    x;
849 +        double  d;
850  
851          fprtresolu(hresolu, vresolu, stdout);
852          for (y = vresolu-1; y >= 0; y--)
853 <                if (fwritecolrs(pscan(y), hresolu, stdout) < 0)
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  
# Line 813 | Line 898 | char   *fname;
898   }
899  
900  
816 isfloat(s)                              /* see if string is floating number */
817 register char   *s;
818 {
819        for ( ; *s; s++)
820                if ((*s < '0' || *s > '9') && *s != '.' && *s != '-'
821                                && *s != 'e' && *s != 'E' && *s != '+')
822                        return(0);
823        return(1);
824 }
825
826
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  
# Line 929 | Line 1005 | clearqueue()                           /* process queue */
1005                          fbp[1] *= ourexp;
1006                          fbp[2] *= ourexp;
1007                  }
1008 <                setcolr(pscan(queue[i][1])[queue[i][0]],
1009 <                                fbp[0], fbp[1], fbp[2]);
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          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines