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.17 by greg, Mon Jan 8 14:47:15 1990 UTC vs.
Revision 1.25 by greg, Tue Mar 6 11:14:36 1990 UTC

# Line 10 | Line 10 | static char SCCSid[] = "$SunId$ LBL";
10  
11   #include "standard.h"
12  
13 + #include <fcntl.h>
14 +
15   #include "view.h"
16  
17   #include "color.h"
18  
19 + #include "random.h"
20 +
21 + #ifndef BSD
22 + #define vfork           fork
23 + #endif
24 +
25   #define pscan(y)        (ourpict+(y)*hresolu)
26   #define zscan(y)        (ourzbuf+(y)*hresolu)
27  
28 + #define PMASK           0xfffffff       /* probability mask */
29 + #define sampval(x)      (long)((x)*(PMASK+1)+.5)
30 + #define samp(p)         ((p) > (random()&PMASK))
31 +
32   #define F_FORE          1               /* fill foreground */
33   #define F_BACK          2               /* fill background */
34  
35   #define PACKSIZ         42              /* calculation packet size */
36  
37 < #define RTCOM           "rtrace -h -ovl -fff -x %d %s"
37 > #define RTCOM           "rtrace -h -ovl -fff %s"
38  
39   #define ABS(x)          ((x)>0?(x):-(x))
40  
# Line 31 | Line 43 | struct position {int x,y; float z;};
43   VIEW    ourview = STDVIEW;              /* desired view */
44   int     hresolu = 512;                  /* horizontal resolution */
45   int     vresolu = 512;                  /* vertical resolution */
46 + double  pixaspect = 1.0;                /* pixel aspect ratio */
47  
48   double  zeps = .02;                     /* allowed z epsilon */
49  
# Line 39 | Line 52 | float  *ourzbuf;                       /* corresponding z-buffer */
52  
53   char    *progname;
54  
55 < int     fill = F_FORE|F_BACK;           /* selected fill algorithm */
55 > int     fillo = F_FORE|F_BACK;          /* selected fill options */
56 > long    sampprob = 0;                   /* sample probability */
57   extern int      backfill(), rcalfill(); /* fill functions */
58 < int     (*deffill)() = backfill;        /* selected fill function */
58 > int     (*fillfunc)() = backfill;       /* selected fill function */
59   COLR    backcolr = BLKCOLR;             /* background color */
60   double  backz = 0.0;                    /* background z value */
61   int     normdist = 1;                   /* normalized distance? */
# Line 91 | Line 105 | char   *argv[];
105                          switch (argv[i][2]) {
106                          case '0':                               /* none */
107                                  check(3,0);
108 <                                fill = 0;
108 >                                fillo = 0;
109                                  break;
110                          case 'f':                               /* foreground */
111                                  check(3,0);
112 <                                fill = F_FORE;
112 >                                fillo = F_FORE;
113                                  break;
114                          case 'b':                               /* background */
115                                  check(3,0);
116 <                                fill = F_BACK;
116 >                                fillo = F_BACK;
117                                  break;
118                          case 'a':                               /* all */
119                                  check(3,0);
120 <                                fill = F_FORE|F_BACK;
120 >                                fillo = F_FORE|F_BACK;
121                                  break;
122 +                        case 's':                               /* sample */
123 +                                check(3,1);
124 +                                sampprob = sampval(atof(argv[++i]));
125 +                                break;
126                          case 'c':                               /* color */
127                                  check(3,3);
128 <                                deffill = backfill;
128 >                                fillfunc = backfill;
129                                  setcolr(backcolr, atof(argv[i+1]),
130                                          atof(argv[i+2]), atof(argv[i+3]));
131                                  i += 3;
132                                  break;
133                          case 'z':                               /* z value */
134                                  check(3,1);
135 <                                deffill = backfill;
135 >                                fillfunc = backfill;
136                                  backz = atof(argv[++i]);
137                                  break;
138                          case 'r':                               /* rtrace */
139                                  check(3,1);
140 <                                deffill = rcalfill;
140 >                                fillfunc = rcalfill;
141                                  calstart(RTCOM, argv[++i]);
142                                  break;
143                          default:
# Line 138 | Line 156 | char   *argv[];
156                          check(2,1);
157                          vresolu = atoi(argv[++i]);
158                          break;
159 +                case 'p':                               /* pixel aspect */
160 +                        check(2,1);
161 +                        pixaspect = atof(argv[++i]);
162 +                        break;
163                  case 'v':                               /* view file */
164                          if (argv[i][2] != 'f')
165                                  goto badopt;
166                          check(3,1);
167 <                        gotvfile = viewfile(argv[++i], &ourview);
167 >                        gotvfile = viewfile(argv[++i], &ourview, 0, 0);
168                          if (gotvfile < 0) {
169                                  perror(argv[i]);
170                                  exit(1);
# Line 167 | Line 189 | char   *argv[];
189                  fprintf(stderr, "%s: %s\n", progname, err);
190                  exit(1);
191          }
192 +        normaspect(viewaspect(&ourview), &pixaspect, &hresolu, &vresolu);
193                                                  /* allocate frame */
194          ourpict = (COLR *)malloc(hresolu*vresolu*sizeof(COLR));
195          ourzbuf = (float *)calloc(hresolu*vresolu,sizeof(float));
# Line 176 | Line 199 | char   *argv[];
199          for ( ; i < argc; i += 2)
200                  addpicture(argv[i], argv[i+1]);
201                                                          /* fill in spaces */
202 <        if (fill&F_BACK)
203 <                backpicture();
202 >        if (fillo&F_BACK)
203 >                backpicture(fillfunc, sampprob);
204          else
205 <                fillpicture();
205 >                fillpicture(fillfunc);
206                                                          /* close calculation */
207          caldone();
208                                                          /* add to header */
209          printargs(argc, argv, stdout);
210          if (gotvfile) {
211 <                printf(VIEWSTR);
211 >                fputs(VIEWSTR, stdout);
212                  fprintview(&ourview, stdout);
213 <                printf("\n");
213 >                putc('\n', stdout);
214          }
215 <        if (ourexp > 0 && ourexp != 1.0)
215 >        if (pixaspect < .99 || pixaspect > 1.01)
216 >                fputaspect(pixaspect, stdout);
217 >        if (ourexp > 0 && (ourexp < .995 || ourexp > 1.005))
218                  fputexpos(ourexp, stdout);
219 <        printf("\n");
219 >        putc('\n', stdout);
220                                                          /* write picture */
221          writepicture();
222                                                          /* write z file */
# Line 211 | Line 236 | userr:
236   headline(s)                             /* process header string */
237   char    *s;
238   {
239 <        static char     *altname[] = {"rview","rpict","pinterp",VIEWSTR,NULL};
239 >        static char     *altname[] = {VIEWSTR,"rpict","rview","pinterp",NULL};
240          register char   **an;
241  
242 <        printf("\t%s", s);
242 >        putc('\t', stdout);
243 >        fputs(s, stdout);
244  
245          if (isexpos(s)) {
246                  theirexp *= exposval(s);
# Line 233 | Line 259 | addpicture(pfile, zspec)               /* add picture to output */
259   char    *pfile, *zspec;
260   {
261          extern double   atof();
262 <        FILE    *pfp, *zfp;
262 >        FILE    *pfp;
263 >        int     zfd;
264          char    *err;
265          COLR    *scanin;
266          float   *zin;
# Line 271 | Line 298 | char   *pfile, *zspec;
298          if (scanin == NULL || zin == NULL || plast == NULL)
299                  syserror();
300                                          /* get z specification or file */
301 <        if ((zfp = fopen(zspec, "r")) == NULL) {
301 >        if ((zfd = open(zspec, O_RDONLY)) == -1) {
302                  double  zvalue;
303                  register int    x;
304                  if (!isfloat(zspec) || (zvalue = atof(zspec)) <= 0.0) {
# Line 287 | Line 314 | char   *pfile, *zspec;
314                          fprintf(stderr, "%s: read error\n", pfile);
315                          exit(1);
316                  }
317 <                if (zfp != NULL
318 <                        && fread(zin,sizeof(float),thresolu,zfp) < thresolu) {
317 >                if (zfd != -1 && read(zfd,(char *)zin,thresolu*sizeof(float))
318 >                                < thresolu*sizeof(float)) {
319                          fprintf(stderr, "%s: read error\n", zspec);
320                          exit(1);
321                  }
# Line 299 | Line 326 | char   *pfile, *zspec;
326          free((char *)zin);
327          free((char *)plast);
328          fclose(pfp);
329 <        if (zfp != NULL)
330 <                fclose(zfp);
329 >        if (zfd != -1)
330 >                close(zfd);
331   }
332  
333  
# Line 351 | Line 378 | struct position        *lasty;         /* input/output */
378          struct position lastx, newpos;
379          register int    x;
380  
381 +        lastx.z = 0;
382          for (x = thresolu-1; x >= 0; x--) {
383                  pos[0] = (x+.5)/thresolu + theirview.hoff - .5;
384                  pos[1] = (y+.5)/tvresolu + theirview.voff - .5;
# Line 403 | Line 431 | double z;
431          register int    x, y;                   /* final position */
432  
433                                          /* compute vector p0p1 */
434 <        if (fill&F_FORE && ABS(p1->z-p0->z) <= zt) {
434 >        if (fillo&F_FORE && ABS(p1->z-p0->z) <= zt) {
435                  s1x = p1->x - p0->x;
436                  s1y = p1->y - p0->y;
437                  l1 = ABS(s1x);
# Line 414 | Line 442 | double z;
442                  p1isy = -1;
443          }
444                                          /* compute vector p0p2 */
445 <        if (fill&F_FORE && ABS(p2->z-p0->z) <= zt) {
445 >        if (fillo&F_FORE && ABS(p2->z-p0->z) <= zt) {
446                  s2x = p2->x - p0->x;
447                  s2y = p2->y - p0->y;
448                  if (p1isy == 1)
# Line 443 | Line 471 | double z;
471   }
472  
473  
474 < backpicture()                           /* background fill algorithm */
474 > backpicture(fill, prob)                 /* background fill algorithm */
475 > int     (*fill)();
476 > long    prob;
477   {
478          int     *yback, xback;
479          int     y;
# Line 495 | Line 525 | backpicture()                          /* background fill algorithm */
525                                  }
526                                  /*
527                                   * Check to see if we have no background for
528 <                                 * this pixel.  If not, use background color.
528 >                                 * this pixel.  If not, or sampling was
529 >                                 * requested, use the given fill function.
530                                   */
531 <                                if (xback < 0 && yback[x] < 0) {
532 <                                        (*deffill)(x,y);
531 >                                if ((xback < 0 && yback[x] < 0) || samp(prob)) {
532 >                                        (*fill)(x,y);
533                                          continue;
534                                  }
535                                  /*
# Line 525 | Line 556 | backpicture()                          /* background fill algorithm */
556   }
557  
558  
559 < fillpicture()                           /* paint in empty pixels with default */
559 > fillpicture(fill)               /* paint in empty pixels using fill */
560 > int     (*fill)();
561   {
562          register int    x, y;
563  
564          for (y = 0; y < vresolu; y++)
565                  for (x = 0; x < hresolu; x++)
566                          if (zscan(y)[x] <= 0)
567 <                                (*deffill)(x,y);
567 >                                (*fill)(x,y);
568   }
569  
570  
# Line 552 | Line 584 | char   *fname;
584   {
585          extern double   sqrt();
586          int     donorm = normdist && ourview.type == VT_PER;
587 <        FILE    *fp;
587 >        int     fd;
588          int     y;
589          float   *zout;
590  
591 <        if ((fp = fopen(fname, "w")) == NULL) {
591 >        if ((fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1) {
592                  perror(fname);
593                  exit(1);
594          }
# Line 576 | Line 608 | char   *fname;
608                          }
609                  } else
610                          zout = zscan(y);
611 <                if (fwrite(zout, sizeof(float), hresolu, fp) < hresolu) {
611 >                if (write(fd, (char *)zout, hresolu*sizeof(float))
612 >                                < hresolu*sizeof(float)) {
613                          perror(fname);
614                          exit(1);
615                  }
616          }
617          if (donorm)
618                  free((char *)zout);
619 <        fclose(fp);
619 >        close(fd);
620   }
621  
622  
# Line 618 | Line 651 | char   *prog, *args;
651                  fprintf(stderr, "%s: too many calculations\n", progname);
652                  exit(1);
653          }
654 <        sprintf(combuf, prog, PACKSIZ, args);
654 >        sprintf(combuf, prog, args);
655          if (pipe(p0) < 0 || pipe(p1) < 0)
656                  syserror();
657          if ((childpid = vfork()) == 0) {        /* fork calculation */
# Line 654 | Line 687 | caldone()                              /* done with calculation */
687  
688          if (childpid == -1)
689                  return;
657        if (fclose(psend) == EOF)
658                syserror();
690          clearqueue();
691 +        fclose(psend);
692          fclose(precv);
693          while ((pid = wait(0)) != -1 && pid != childpid)
694                  ;
# Line 667 | Line 699 | caldone()                              /* done with calculation */
699   rcalfill(x, y)                          /* fill with ray-calculated pixel */
700   int     x, y;
701   {
702 <        FVECT   orig, dir;
671 <        float   outbuf[6];
672 <
673 <        if (queuesiz >= PACKSIZ) {      /* flush queue */
674 <                if (fflush(psend) == EOF)
675 <                        syserror();
702 >        if (queuesiz >= PACKSIZ)        /* flush queue if needed */
703                  clearqueue();
704 <        }
678 <                                        /* send new ray */
679 <        viewray(orig, dir, &ourview, (x+.5)/hresolu, (y+.5)/vresolu);
680 <        outbuf[0] = orig[0]; outbuf[1] = orig[1]; outbuf[2] = orig[2];
681 <        outbuf[3] = dir[0]; outbuf[4] = dir[1]; outbuf[5] = dir[2];
682 <        if (fwrite(outbuf, sizeof(float), 6, psend) < 6)
683 <                syserror();
684 <                                        /* remember it */
704 >                                        /* add position to queue */
705          queue[queuesiz][0] = x;
706          queue[queuesiz][1] = y;
707          queuesiz++;
708   }
709  
710  
711 < clearqueue()                            /* get results from queue */
711 > clearqueue()                            /* process queue */
712   {
713 <        float   inbuf[4];
713 >        FVECT   orig, dir;
714 >        float   fbuf[6];
715          register int    i;
716  
717          for (i = 0; i < queuesiz; i++) {
718 <                if (fread(inbuf, sizeof(float), 4, precv) < 4) {
718 >                viewray(orig, dir, &ourview,
719 >                                (queue[i][0]+.5)/hresolu,
720 >                                (queue[i][1]+.5)/vresolu);
721 >                fbuf[0] = orig[0]; fbuf[1] = orig[1]; fbuf[2] = orig[2];
722 >                fbuf[3] = dir[0]; fbuf[4] = dir[1]; fbuf[5] = dir[2];
723 >                fwrite((char *)fbuf, sizeof(float), 6, psend);
724 >        }
725 >                                        /* flush output and get results */
726 >        fbuf[3] = fbuf[4] = fbuf[5] = 0.0;              /* mark */
727 >        fwrite((char *)fbuf, sizeof(float), 6, psend);
728 >        if (fflush(psend) == EOF)
729 >                syserror();
730 >        for (i = 0; i < queuesiz; i++) {
731 >                if (fread((char *)fbuf, sizeof(float), 4, precv) < 4) {
732                          fprintf(stderr, "%s: read error in clearqueue\n",
733                                          progname);
734                          exit(1);
735                  }
736                  if (ourexp > 0 && ourexp != 1.0) {
737 <                        inbuf[0] *= ourexp;
738 <                        inbuf[1] *= ourexp;
739 <                        inbuf[2] *= ourexp;
737 >                        fbuf[0] *= ourexp;
738 >                        fbuf[1] *= ourexp;
739 >                        fbuf[2] *= ourexp;
740                  }
741                  setcolr(pscan(queue[i][1])[queue[i][0]],
742 <                                inbuf[0], inbuf[1], inbuf[2]);
743 <                zscan(queue[i][1])[queue[i][0]] = inbuf[3];
742 >                                fbuf[0], fbuf[1], fbuf[2]);
743 >                zscan(queue[i][1])[queue[i][0]] = fbuf[3];
744          }
745          queuesiz = 0;
746   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines