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.8 by greg, Wed Jan 3 09:37:16 1990 UTC vs.
Revision 1.13 by greg, Thu Jan 4 16:52:05 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(), calfill();  /* 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     normdist = 1;                   /* normalized distance? */
49  
50 + FILE    *psend, *precv;                 /* pipes to/from fill calculation */
51 + int     childpid;                       /* child's process id */
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 51 | Line 72 | char   *argv[];
72                          check(2,1);
73                          zeps = atof(argv[++i]);
74                          break;
75 +                case 'n':                               /* dist. normalized? */
76 +                        check(2,0);
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 = calfill;
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 109 | Line 177 | char   *argv[];
177                          break;
178                  default:
179                  badopt:
180 <                        fprintf(stderr, "%s: unknown option '%s'\n",
180 >                        fprintf(stderr, "%s: command line error at '%s'\n",
181                                          progname, argv[i]);
182 <                        exit(1);
182 >                        goto userr;
183                  }
184                                                  /* check arguments */
185 <        if (argc-i < 2 || (argc-i)%2) {
186 <                fprintf(stderr, "Usage: %s [view args] pfile zfile ..\n",
119 <                                progname);
120 <                exit(1);
121 <        }
185 >        if ((argc-i)%2)
186 >                goto userr;
187                                                  /* set view */
188          if (err = setview(&ourview)) {
189                  fprintf(stderr, "%s: %s\n", progname, err);
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 135 | 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 >        if (deffill == calfill)
209 >                caldone();
210                                                          /* add to header */
211          printargs(argc, argv, stdout);
212          if (gotvfile) {
# Line 144 | Line 215 | char   *argv[];
215                  printf("\n");
216          }
217          printf("\n");
218 <                                                        /* write output */
218 >                                                        /* write picture */
219          writepicture();
220 +                                                        /* write z file */
221 +        if (zfile != NULL)
222 +                writedistance(zfile);
223  
224          exit(0);
225 + userr:
226 +        fprintf(stderr,
227 +        "Usage: %s [view opts][-t zthresh][-z zout][-fT][-n] pfile zspec ..\n",
228 +                        progname);
229 +        exit(1);
230   #undef check
231   }
232  
# Line 169 | Line 248 | char   *s;
248   }
249  
250  
251 < addpicture(pfile, zfile)                /* add picture to output */
252 < char    *pfile, *zfile;
251 > addpicture(pfile, zspec)                /* add picture to output */
252 > char    *pfile, *zspec;
253   {
254 +        extern double   atof();
255          FILE    *pfp, *zfp;
256          char    *err;
257          COLR    *scanin;
258 <        float   *zin, *zout;
259 <        int     *pout;
180 <        int     xres, yres;
258 >        float   *zin, *zlast;
259 >        int     *plast;
260          int     y;
261 <                                        /* open input files */
261 >                                        /* open picture file */
262          if ((pfp = fopen(pfile, "r")) == NULL) {
263                  perror(pfile);
264                  exit(1);
265          }
187        if ((zfp = fopen(zfile, "r")) == NULL) {
188                perror(zfile);
189                exit(1);
190        }
266                                          /* get header and view */
267          printf("%s:\n", pfile);
268          gotview = 0;
269          getheader(pfp, headline);
270 <        if (!gotview || fgetresolu(&xres, &yres, pfp) != (YMAJOR|YDECR)) {
270 >        if (!gotview || fgetresolu(&theirview.hresolu, &theirview.vresolu, pfp)
271 >                        != (YMAJOR|YDECR)) {
272                  fprintf(stderr, "%s: picture view error\n", pfile);
273                  exit(1);
274          }
199        theirview.hresolu = xres;
200        theirview.vresolu = yres;
275          if (err = setview(&theirview)) {
276                  fprintf(stderr, "%s: %s\n", pfile, err);
277                  exit(1);
# Line 205 | Line 279 | char   *pfile, *zfile;
279                                          /* compute transformation */
280          pixform(theirs2ours, &theirview, &ourview);
281                                          /* allocate scanlines */
282 <        scanin = (COLR *)malloc(xres*sizeof(COLR));
283 <        zin = (float *)malloc(xres*sizeof(float));
284 <        zout = (float *)calloc(xres, sizeof(float));
285 <        pout = (int *)calloc(xres, sizeof(int));
286 <        if (scanin == NULL || zin == NULL || zout == NULL || pout == NULL) {
282 >        scanin = (COLR *)malloc(theirview.hresolu*sizeof(COLR));
283 >        zin = (float *)malloc(theirview.hresolu*sizeof(float));
284 >        plast = (int *)calloc(theirview.hresolu, sizeof(int));
285 >        zlast = (float *)calloc(theirview.hresolu, sizeof(float));
286 >        if (scanin == NULL || zin == NULL || plast == NULL || zlast == NULL) {
287                  perror(progname);
288                  exit(1);
289          }
290 +                                        /* get z specification or file */
291 +        if ((zfp = fopen(zspec, "r")) == NULL) {
292 +                double  zvalue;
293 +                register int    x;
294 +                if (!isfloat(zspec) || (zvalue = atof(zspec)) <= 0.0) {
295 +                        perror(zspec);
296 +                        exit(1);
297 +                }
298 +                for (x = 0; x < theirview.hresolu; x++)
299 +                        zin[x] = zvalue;
300 +        }
301                                          /* load image */
302 <        for (y = yres-1; y >= 0; y--) {
303 <                if (freadcolrs(scanin, xres, pfp) < 0) {
302 >        for (y = theirview.vresolu-1; y >= 0; y--) {
303 >                if (freadcolrs(scanin, theirview.hresolu, pfp) < 0) {
304                          fprintf(stderr, "%s: read error\n", pfile);
305                          exit(1);
306                  }
307 <                if (fread(zin, sizeof(float), xres, zfp) < xres) {
308 <                        fprintf(stderr, "%s: read error\n", zfile);
307 >                if (zfp != NULL
308 >                        && fread(zin,sizeof(float),theirview.hresolu,zfp)
309 >                                < theirview.hresolu) {
310 >                        fprintf(stderr, "%s: read error\n", zspec);
311                          exit(1);
312                  }
313 <                addscanline(y, scanin, zin, pout, zout);
313 >                addscanline(y, scanin, zin, plast, zlast);
314          }
315                                          /* clean up */
316          free((char *)scanin);
317          free((char *)zin);
318 <        free((char *)pout);
319 <        free((char *)zout);
318 >        free((char *)plast);
319 >        free((char *)zlast);
320          fclose(pfp);
321 <        fclose(zfp);
321 >        if (zfp != NULL)
322 >                fclose(zfp);
323   }
324  
325  
# Line 275 | Line 363 | addscanline(y, pline, zline, lasty, lastyz)    /* add sca
363   int     y;
364   COLR    *pline;
365   float   *zline;
366 < int     *lasty;
367 < float   *lastyz;
366 > int     *lasty;                 /* input/output */
367 > float   *lastyz;                /* input/output */
368   {
369 <        extern double   sqrt(), fabs();
369 >        extern double   sqrt();
370          double  pos[3];
371          int     lastx = 0;
372          double  lastxz = 0;
# Line 291 | Line 379 | float  *lastyz;
379                  pos[1] = y - .5*(theirview.vresolu-1);
380                  pos[2] = zline[x];
381                  if (theirview.type == VT_PER) {
382 <                        /*
383 <                         * The following (single) statement can go
296 <                         * if z is along the view direction rather
297 <                         * than an eye ray.
298 <                         */
299 <                        pos[2] /= sqrt( 1.
382 >                        if (normdist)   /* adjust for eye-ray distance */
383 >                                pos[2] /= sqrt( 1.
384                                          + pos[0]*pos[0]*theirview.vhn2
385                                          + pos[1]*pos[1]*theirview.vvn2 );
386                          pos[0] *= pos[2];
# Line 317 | Line 401 | float  *lastyz;
401                                          /* add pixel to our image */
402                  zt = 2.*zeps*zline[x];
403                  addpixel(xpos, ypos,
404 <                        (fabs(zline[x]-lastxz) <= zt) ? lastx - xpos : 1,
405 <                        (fabs(zline[x]-lastyz[x]) <= zt) ? lasty[x] - ypos : 1,
404 >                        (fill&F_FORE && ABS(zline[x]-lastxz) <= zt)
405 >                                ? lastx - xpos : 1,
406 >                        (fill&F_FORE && ABS(zline[x]-lastyz[x]) <= zt)
407 >                                ? lasty[x] - ypos : 1,
408                          pline[x], pos[2]);
409                  lastx = xpos;
410                  lasty[x] = ypos;
# Line 356 | Line 442 | double z;
442   }
443  
444  
445 < fillpicture()                           /* fill in empty spaces */
445 > backpicture()                           /* background fill algorithm */
446   {
447          int     *yback, xback;
448          int     y;
# Line 399 | Line 485 | fillpicture()                          /* fill in empty spaces */
485                                   * Next, find background from left or right.
486                                   */
487                                  if (xback == -2) {
488 <                                        for (i = x+1; x < ourview.hresolu; i++)
488 >                                        for (i = x+1; i < ourview.hresolu; i++)
489                                                  if (zscan(y)[i] > 0)
490                                                          break;
491                                          if (i < ourview.hresolu
# Line 408 | Line 494 | fillpicture()                          /* fill in empty spaces */
494                                          else
495                                                  xback = x-1;
496                                  }
411                                if (xback < 0 && yback[x] < 0)
412                                        continue;       /* no background */
497                                  /*
498 +                                 * Check to see if we have no background for
499 +                                 * this pixel.  If not, use background color.
500 +                                 */
501 +                                if (xback < 0 && yback[x] < 0) {
502 +                                        (*deffill)(x,y);
503 +                                        continue;
504 +                                }
505 +                                /*
506                                   * Compare, and use the background that is
507                                   * farther, unless one of them is next to us.
508                                   */
509 <                                if (yback[x] < 0 || ABS(x-xback) <= 1
509 >                                if ( yback[x] < 0
510 >                                        || (xback >= 0 && ABS(x-xback) <= 1)
511                                          || ( ABS(y-yback[x]) > 1
512 <                                && zscan(yback[x])[x] < zscan(y)[xback] ))
512 >                                                && zscan(yback[x])[x]
513 >                                                < zscan(y)[xback] ) ) {
514                                          copycolr(pscan(y)[x],pscan(y)[xback]);
515 <                                else
515 >                                        zscan(y)[x] = zscan(y)[xback];
516 >                                } else {
517                                          copycolr(pscan(y)[x],pscan(yback[x])[x]);
518 +                                        zscan(y)[x] = zscan(yback[x])[x];
519 +                                }
520                          } else {                                /* full pixel */
521                                  yback[x] = -2;
522                                  xback = -2;
# Line 429 | Line 526 | fillpicture()                          /* fill in empty spaces */
526   }
527  
528  
529 + fillpicture()                           /* paint in empty pixels with default */
530 + {
531 +        register int    x, y;
532 +
533 +        for (y = 0; y < ourview.vresolu; y++)
534 +                for (x = 0; x < ourview.hresolu; x++)
535 +                        if (zscan(y)[x] <= 0)
536 +                                (*deffill)(x,y);
537 + }
538 +
539 +
540   writepicture()                          /* write out picture */
541   {
542          int     y;
# Line 439 | Line 547 | writepicture()                         /* write out picture */
547                          perror(progname);
548                          exit(1);
549                  }
550 + }
551 +
552 +
553 + writedistance(fname)                    /* write out z file */
554 + char    *fname;
555 + {
556 +        extern double   sqrt();
557 +        int     donorm = normdist && ourview.type == VT_PER;
558 +        FILE    *fp;
559 +        int     y;
560 +        float   *zout;
561 +
562 +        if ((fp = fopen(fname, "w")) == NULL) {
563 +                perror(fname);
564 +                exit(1);
565 +        }
566 +        if (donorm
567 +        && (zout = (float *)malloc(ourview.hresolu*sizeof(float))) == NULL) {
568 +                perror(progname);
569 +                exit(1);
570 +        }
571 +        for (y = ourview.vresolu-1; y >= 0; y--) {
572 +                if (donorm) {
573 +                        double  vx, yzn2;
574 +                        register int    x;
575 +                        yzn2 = y - .5*(ourview.vresolu-1);
576 +                        yzn2 = 1. + yzn2*yzn2*ourview.vvn2;
577 +                        for (x = 0; x < ourview.hresolu; x++) {
578 +                                vx = x - .5*(ourview.hresolu-1);
579 +                                zout[x] = zscan(y)[x]
580 +                                        * sqrt(vx*vx*ourview.vhn2 + yzn2);
581 +                        }
582 +                } else
583 +                        zout = zscan(y);
584 +                if (fwrite(zout, sizeof(float), ourview.hresolu, fp)
585 +                                < ourview.hresolu) {
586 +                        perror(fname);
587 +                        exit(1);
588 +                }
589 +        }
590 +        if (donorm)
591 +                free((char *)zout);
592 +        fclose(fp);
593 + }
594 +
595 +
596 + isfloat(s)                              /* see if string is floating number */
597 + register char   *s;
598 + {
599 +        for ( ; *s; s++)
600 +                if ((*s < '0' || *s > '9') && *s != '.' && *s != '-'
601 +                                && *s != 'e' && *s != 'E' && *s != '+')
602 +                        return(0);
603 +        return(1);
604 + }
605 +
606 +
607 + backfill(x, y)                          /* fill pixel with background */
608 + int     x, y;
609 + {
610 +        register BYTE   *dest = pscan(y)[x];
611 +
612 +        copycolr(dest, backcolr);
613 +        zscan(y)[x] = backz;
614 + }
615 +
616 +
617 + calstart(prog, args)                    /* start fill calculation */
618 + char    *prog, *args;
619 + {
620 +        char    combuf[512];
621 +        int     p0[2], p1[2];
622 +
623 +        sprintf(combuf, prog, PACKSIZ, args);
624 +        if (pipe(p0) < 0 || pipe(p1) < 0)
625 +                goto syserr;
626 +        if ((childpid = vfork()) == 0) {        /* fork calculation */
627 +                close(p0[1]);
628 +                close(p1[0]);
629 +                if (p0[0] != 0) {
630 +                        dup2(p0[0], 0);
631 +                        close(p0[0]);
632 +                }
633 +                if (p1[1] != 1) {
634 +                        dup2(p1[1], 1);
635 +                        close(p1[1]);
636 +                }
637 +                execl("/bin/sh", "sh", "-c", combuf, 0);
638 +                perror("/bin/sh");
639 +                _exit(127);
640 +        }
641 +        if (childpid < 0)
642 +                goto syserr;
643 +        close(p0[0]);
644 +        close(p1[1]);
645 +        if ((psend = fdopen(p0[1], "w")) == NULL)
646 +                goto syserr;
647 +        if ((precv = fdopen(p1[0], "r")) == NULL)
648 +                goto syserr;
649 +        queuesiz = 0;
650 +        return;
651 + syserr:
652 +        perror(progname);
653 +        exit(1);
654 + }
655 +
656 +
657 + caldone()                               /* done with calculation */
658 + {
659 +        int     pid;
660 +
661 +        fclose(psend);
662 +        clearqueue();
663 +        fclose(precv);
664 +        while ((pid = wait(0)) != -1 && pid != childpid)
665 +                ;
666 + }
667 +
668 +
669 + calfill(x, y)                           /* fill with calculated pixel */
670 + int     x, y;
671 + {
672 +        FVECT   orig, dir;
673 +        float   outbuf[6];
674 +
675 +        if (queuesiz >= PACKSIZ) {      /* flush queue */
676 +                fflush(psend);
677 +                clearqueue();
678 +        }
679 +                                        /* send new ray */
680 +        rayview(orig, dir, &ourview, x+.5, y+.5);
681 +        outbuf[0] = orig[0]; outbuf[1] = orig[1]; outbuf[2] = orig[2];
682 +        outbuf[3] = dir[0]; outbuf[4] = dir[1]; outbuf[5] = dir[2];
683 +        fwrite(outbuf, sizeof(float), 6, psend);
684 +                                        /* remember it */
685 +        queue[queuesiz][0] = x;
686 +        queue[queuesiz][1] = y;
687 +        queuesiz++;
688 + }
689 +
690 +
691 + clearqueue()                            /* get results from queue */
692 + {
693 +        float   inbuf[4];
694 +        register int    i;
695 +
696 +        for (i = 0; i < queuesiz; i++) {
697 +                fread(inbuf, sizeof(float), 4, precv);
698 +                setcolr(pscan(queue[i][1])[queue[i][0]],
699 +                                inbuf[0], inbuf[1], inbuf[2]);
700 +                zscan(queue[i][1])[queue[i][0]] = inbuf[3];
701 +        }
702 +        queuesiz = 0;
703   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines