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.10 by greg, Wed Jan 3 15:33:44 1990 UTC vs.
Revision 1.14 by greg, Thu Jan 4 17:47:37 1990 UTC

# Line 17 | Line 17 | static char SCCSid[] = "$SunId$ LBL";
17   #define pscan(y)        (ourpict+(y)*ourview.hresolu)
18   #define zscan(y)        (ourzbuf+(y)*ourview.hresolu)
19  
20 + #define F_FORE          1               /* fill foreground */
21 + #define F_BACK          2               /* fill background */
22 +
23 + #define PACKSIZ         42              /* calculation packet size */
24 +
25 + #define RTCOM           "rtrace -h -ovl -fff -x %d %s"
26 +
27   #define ABS(x)          ((x)>0?(x):-(x))
28  
29   VIEW    ourview = STDVIEW(512);         /* desired view */
# Line 31 | Line 38 | char   *progname;
38   VIEW    theirview = STDVIEW(512);       /* input view */
39   int     gotview;                        /* got input view? */
40  
41 + int     fill = F_FORE|F_BACK;           /* selected fill algorithm */
42 + extern int      backfill(), rcalfill(); /* fill functions */
43 + int     (*deffill)() = backfill;        /* selected fill function */
44 + COLR    backcolr = BLKCOLR;             /* background color */
45 + double  backz = 0.0;                    /* background z value */
46 +
47   double  theirs2ours[4][4];              /* transformation matrix */
48 < int     regdist = 0;                    /* regular distance? */
48 > int     normdist = 1;                   /* normalized distance? */
49  
50 + int     childpid = -1;                  /* id of fill process */
51 + FILE    *psend, *precv;                 /* pipes to/from fill calculation */
52 + int     queue[PACKSIZ][2];              /* pending pixels */
53 + int     queuesiz;                       /* number of pixels pending */
54  
55 +
56   main(argc, argv)                        /* interpolate pictures */
57   int     argc;
58   char    *argv[];
59   {
60   #define check(olen,narg)        if (argv[i][olen] || narg >= argc-i) goto badopt
61 +        extern double   atof();
62          int     gotvfile = 0;
63 +        char    *zfile = NULL;
64          char    *err;
65          int     i;
66  
# Line 52 | Line 72 | char   *argv[];
72                          check(2,1);
73                          zeps = atof(argv[++i]);
74                          break;
75 <                case 'r':                               /* regular distance */
75 >                case 'n':                               /* dist. normalized? */
76                          check(2,0);
77 <                        regdist = !regdist;
77 >                        normdist = !normdist;
78                          break;
79 +                case 'f':                               /* fill type */
80 +                        switch (argv[i][2]) {
81 +                        case '0':                               /* none */
82 +                                check(3,0);
83 +                                fill = 0;
84 +                                break;
85 +                        case 'f':                               /* foreground */
86 +                                check(3,0);
87 +                                fill = F_FORE;
88 +                                break;
89 +                        case 'b':                               /* background */
90 +                                check(3,0);
91 +                                fill = F_BACK;
92 +                                break;
93 +                        case 'a':                               /* all */
94 +                                check(3,0);
95 +                                fill = F_FORE|F_BACK;
96 +                                break;
97 +                        case 'c':                               /* color */
98 +                                check(3,3);
99 +                                deffill = backfill;
100 +                                setcolr(backcolr, atof(argv[i+1]),
101 +                                        atof(argv[i+2]), atof(argv[i+3]));
102 +                                i += 3;
103 +                                break;
104 +                        case 'z':                               /* z value */
105 +                                check(3,1);
106 +                                deffill = backfill;
107 +                                backz = atof(argv[++i]);
108 +                                break;
109 +                        case 'r':                               /* rtrace */
110 +                                check(3,1);
111 +                                deffill = rcalfill;
112 +                                calstart(RTCOM, argv[++i]);
113 +                                break;
114 +                        default:
115 +                                goto badopt;
116 +                        }
117 +                        break;
118 +                case 'z':                               /* z file */
119 +                        check(2,1);
120 +                        zfile = argv[++i];
121 +                        break;
122                  case 'x':                               /* x resolution */
123                          check(2,1);
124                          ourview.hresolu = atoi(argv[++i]);
# Line 119 | Line 182 | char   *argv[];
182                          goto userr;
183                  }
184                                                  /* check arguments */
185 <        if (argc-i < 2 || (argc-i)%2)
185 >        if ((argc-i)%2)
186                  goto userr;
187                                                  /* set view */
188          if (err = setview(&ourview)) {
# Line 127 | Line 190 | char   *argv[];
190                  exit(1);
191          }
192                                                  /* allocate frame */
193 <        ourpict = (COLR *)calloc(ourview.hresolu*ourview.vresolu,sizeof(COLR));
193 >        ourpict = (COLR *)malloc(ourview.hresolu*ourview.vresolu*sizeof(COLR));
194          ourzbuf = (float *)calloc(ourview.hresolu*ourview.vresolu,sizeof(float));
195          if (ourpict == NULL || ourzbuf == NULL) {
196                  perror(progname);
# Line 137 | Line 200 | char   *argv[];
200          for ( ; i < argc; i += 2)
201                  addpicture(argv[i], argv[i+1]);
202                                                          /* fill in spaces */
203 <        fillpicture();
203 >        if (fill&F_BACK)
204 >                backpicture();
205 >        else
206 >                fillpicture();
207 >                                                        /* close calculation */
208 >        caldone();
209                                                          /* add to header */
210          printargs(argc, argv, stdout);
211          if (gotvfile) {
# Line 146 | Line 214 | char   *argv[];
214                  printf("\n");
215          }
216          printf("\n");
217 <                                                        /* write output */
217 >                                                        /* write picture */
218          writepicture();
219 +                                                        /* write z file */
220 +        if (zfile != NULL)
221 +                writedistance(zfile);
222  
223          exit(0);
224   userr:
225          fprintf(stderr,
226 <        "Usage: %s [view opts][-t zthresh][-r] pfile zspec ..\n",
226 >        "Usage: %s [view opts][-t zthresh][-z zout][-fT][-n] pfile zspec ..\n",
227                          progname);
228          exit(1);
229   #undef check
# Line 294 | Line 365 | float  *zline;
365   int     *lasty;                 /* input/output */
366   float   *lastyz;                /* input/output */
367   {
368 <        extern double   sqrt(), fabs();
368 >        extern double   sqrt();
369          double  pos[3];
370          int     lastx = 0;
371          double  lastxz = 0;
# Line 307 | Line 378 | float  *lastyz;                /* input/output */
378                  pos[1] = y - .5*(theirview.vresolu-1);
379                  pos[2] = zline[x];
380                  if (theirview.type == VT_PER) {
381 <                        if (!regdist)   /* adjust for eye-ray distance */
381 >                        if (normdist)   /* adjust for eye-ray distance */
382                                  pos[2] /= sqrt( 1.
383                                          + pos[0]*pos[0]*theirview.vhn2
384                                          + pos[1]*pos[1]*theirview.vvn2 );
# Line 329 | Line 400 | float  *lastyz;                /* input/output */
400                                          /* add pixel to our image */
401                  zt = 2.*zeps*zline[x];
402                  addpixel(xpos, ypos,
403 <                        (fabs(zline[x]-lastxz) <= zt) ? lastx - xpos : 1,
404 <                        (fabs(zline[x]-lastyz[x]) <= zt) ? lasty[x] - ypos : 1,
403 >                        (fill&F_FORE && ABS(zline[x]-lastxz) <= zt)
404 >                                ? lastx - xpos : 1,
405 >                        (fill&F_FORE && ABS(zline[x]-lastyz[x]) <= zt)
406 >                                ? lasty[x] - ypos : 1,
407                          pline[x], pos[2]);
408                  lastx = xpos;
409                  lasty[x] = ypos;
# Line 368 | Line 441 | double z;
441   }
442  
443  
444 < fillpicture()                           /* fill in empty spaces */
444 > backpicture()                           /* background fill algorithm */
445   {
446          int     *yback, xback;
447          int     y;
# Line 411 | Line 484 | fillpicture()                          /* fill in empty spaces */
484                                   * Next, find background from left or right.
485                                   */
486                                  if (xback == -2) {
487 <                                        for (i = x+1; x < ourview.hresolu; i++)
487 >                                        for (i = x+1; i < ourview.hresolu; i++)
488                                                  if (zscan(y)[i] > 0)
489                                                          break;
490                                          if (i < ourview.hresolu
# Line 420 | Line 493 | fillpicture()                          /* fill in empty spaces */
493                                          else
494                                                  xback = x-1;
495                                  }
423                                if (xback < 0 && yback[x] < 0)
424                                        continue;       /* no background */
496                                  /*
497 +                                 * Check to see if we have no background for
498 +                                 * this pixel.  If not, use background color.
499 +                                 */
500 +                                if (xback < 0 && yback[x] < 0) {
501 +                                        (*deffill)(x,y);
502 +                                        continue;
503 +                                }
504 +                                /*
505                                   * Compare, and use the background that is
506                                   * farther, unless one of them is next to us.
507                                   */
508 <                                if (yback[x] < 0 || ABS(x-xback) <= 1
508 >                                if ( yback[x] < 0
509 >                                        || (xback >= 0 && ABS(x-xback) <= 1)
510                                          || ( ABS(y-yback[x]) > 1
511 <                                && zscan(yback[x])[x] < zscan(y)[xback] ))
511 >                                                && zscan(yback[x])[x]
512 >                                                < zscan(y)[xback] ) ) {
513                                          copycolr(pscan(y)[x],pscan(y)[xback]);
514 <                                else
514 >                                        zscan(y)[x] = zscan(y)[xback];
515 >                                } else {
516                                          copycolr(pscan(y)[x],pscan(yback[x])[x]);
517 +                                        zscan(y)[x] = zscan(yback[x])[x];
518 +                                }
519                          } else {                                /* full pixel */
520                                  yback[x] = -2;
521                                  xback = -2;
# Line 441 | Line 525 | fillpicture()                          /* fill in empty spaces */
525   }
526  
527  
528 + fillpicture()                           /* paint in empty pixels with default */
529 + {
530 +        register int    x, y;
531 +
532 +        for (y = 0; y < ourview.vresolu; y++)
533 +                for (x = 0; x < ourview.hresolu; x++)
534 +                        if (zscan(y)[x] <= 0)
535 +                                (*deffill)(x,y);
536 + }
537 +
538 +
539   writepicture()                          /* write out picture */
540   {
541          int     y;
# Line 454 | Line 549 | writepicture()                         /* write out picture */
549   }
550  
551  
552 + writedistance(fname)                    /* write out z file */
553 + char    *fname;
554 + {
555 +        extern double   sqrt();
556 +        int     donorm = normdist && ourview.type == VT_PER;
557 +        FILE    *fp;
558 +        int     y;
559 +        float   *zout;
560 +
561 +        if ((fp = fopen(fname, "w")) == NULL) {
562 +                perror(fname);
563 +                exit(1);
564 +        }
565 +        if (donorm
566 +        && (zout = (float *)malloc(ourview.hresolu*sizeof(float))) == NULL) {
567 +                perror(progname);
568 +                exit(1);
569 +        }
570 +        for (y = ourview.vresolu-1; y >= 0; y--) {
571 +                if (donorm) {
572 +                        double  vx, yzn2;
573 +                        register int    x;
574 +                        yzn2 = y - .5*(ourview.vresolu-1);
575 +                        yzn2 = 1. + yzn2*yzn2*ourview.vvn2;
576 +                        for (x = 0; x < ourview.hresolu; x++) {
577 +                                vx = x - .5*(ourview.hresolu-1);
578 +                                zout[x] = zscan(y)[x]
579 +                                        * sqrt(vx*vx*ourview.vhn2 + yzn2);
580 +                        }
581 +                } else
582 +                        zout = zscan(y);
583 +                if (fwrite(zout, sizeof(float), ourview.hresolu, fp)
584 +                                < ourview.hresolu) {
585 +                        perror(fname);
586 +                        exit(1);
587 +                }
588 +        }
589 +        if (donorm)
590 +                free((char *)zout);
591 +        fclose(fp);
592 + }
593 +
594 +
595   isfloat(s)                              /* see if string is floating number */
596   register char   *s;
597   {
# Line 462 | Line 600 | register char  *s;
600                                  && *s != 'e' && *s != 'E' && *s != '+')
601                          return(0);
602          return(1);
603 + }
604 +
605 +
606 + backfill(x, y)                          /* fill pixel with background */
607 + int     x, y;
608 + {
609 +        register BYTE   *dest = pscan(y)[x];
610 +
611 +        copycolr(dest, backcolr);
612 +        zscan(y)[x] = backz;
613 + }
614 +
615 +
616 + calstart(prog, args)                    /* start fill calculation */
617 + char    *prog, *args;
618 + {
619 +        char    combuf[512];
620 +        int     p0[2], p1[2];
621 +
622 +        if (childpid != -1) {
623 +                fprintf(stderr, "%s: too many calculations\n", progname);
624 +                exit(1);
625 +        }
626 +        sprintf(combuf, prog, PACKSIZ, args);
627 +        if (pipe(p0) < 0 || pipe(p1) < 0)
628 +                syserror();
629 +        if ((childpid = vfork()) == 0) {        /* fork calculation */
630 +                close(p0[1]);
631 +                close(p1[0]);
632 +                if (p0[0] != 0) {
633 +                        dup2(p0[0], 0);
634 +                        close(p0[0]);
635 +                }
636 +                if (p1[1] != 1) {
637 +                        dup2(p1[1], 1);
638 +                        close(p1[1]);
639 +                }
640 +                execl("/bin/sh", "sh", "-c", combuf, 0);
641 +                perror("/bin/sh");
642 +                _exit(127);
643 +        }
644 +        if (childpid == -1)
645 +                syserror();
646 +        close(p0[0]);
647 +        close(p1[1]);
648 +        if ((psend = fdopen(p0[1], "w")) == NULL)
649 +                syserror();
650 +        if ((precv = fdopen(p1[0], "r")) == NULL)
651 +                syserror();
652 +        queuesiz = 0;
653 + }
654 +
655 +
656 + caldone()                               /* done with calculation */
657 + {
658 +        int     pid;
659 +
660 +        if (childpid == -1)
661 +                return;
662 +        if (fclose(psend) == EOF)
663 +                syserror();
664 +        clearqueue();
665 +        fclose(precv);
666 +        while ((pid = wait(0)) != -1 && pid != childpid)
667 +                ;
668 +        childpid = -1;
669 + }
670 +
671 +
672 + rcalfill(x, y)                          /* fill with ray-calculated pixel */
673 + int     x, y;
674 + {
675 +        FVECT   orig, dir;
676 +        float   outbuf[6];
677 +
678 +        if (queuesiz >= PACKSIZ) {      /* flush queue */
679 +                if (fflush(psend) == EOF)
680 +                        syserror();
681 +                clearqueue();
682 +        }
683 +                                        /* send new ray */
684 +        rayview(orig, dir, &ourview, x+.5, y+.5);
685 +        outbuf[0] = orig[0]; outbuf[1] = orig[1]; outbuf[2] = orig[2];
686 +        outbuf[3] = dir[0]; outbuf[4] = dir[1]; outbuf[5] = dir[2];
687 +        if (fwrite(outbuf, sizeof(float), 6, psend) < 6)
688 +                syserror();
689 +                                        /* remember it */
690 +        queue[queuesiz][0] = x;
691 +        queue[queuesiz][1] = y;
692 +        queuesiz++;
693 + }
694 +
695 +
696 + clearqueue()                            /* get results from queue */
697 + {
698 +        float   inbuf[4];
699 +        register int    i;
700 +
701 +        for (i = 0; i < queuesiz; i++) {
702 +                if (fread(inbuf, sizeof(float), 4, precv) < 4) {
703 +                        fprintf(stderr, "%s: read error in clearqueue\n",
704 +                                        progname);
705 +                        exit(1);
706 +                }
707 +                setcolr(pscan(queue[i][1])[queue[i][0]],
708 +                                inbuf[0], inbuf[1], inbuf[2]);
709 +                zscan(queue[i][1])[queue[i][0]] = inbuf[3];
710 +        }
711 +        queuesiz = 0;
712 + }
713 +
714 +
715 + syserror()                      /* report error and exit */
716 + {
717 +        perror(progname);
718 +        exit(1);
719   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines