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.24 by greg, Mon Feb 26 14:01:44 1990 UTC vs.
Revision 2.2 by greg, Thu Dec 19 14:51:55 1991 UTC

# Line 1 | Line 1
1 + /* Copyright (c) 1991 Regents of the University of California */
2 +
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
5   #endif
# Line 16 | Line 18 | static char SCCSid[] = "$SunId$ LBL";
18  
19   #include "color.h"
20  
21 + #include "resolu.h"
22 +
23   #ifndef BSD
24   #define vfork           fork
25   #endif
# Line 28 | Line 32 | static char SCCSid[] = "$SunId$ LBL";
32  
33   #define PACKSIZ         42              /* calculation packet size */
34  
35 < #define RTCOM           "rtrace -h -ovl -fff -x %d %s"
35 > #define RTCOM           "rtrace -h -ovl -fff %s"
36  
37   #define ABS(x)          ((x)>0?(x):-(x))
38  
# Line 46 | Line 50 | float  *ourzbuf;                       /* corresponding z-buffer */
50  
51   char    *progname;
52  
53 < int     fill = F_FORE|F_BACK;           /* selected fill algorithm */
53 > int     fillo = F_FORE|F_BACK;          /* selected fill options */
54 > int     fillsamp = 0;                   /* sample separation (0 == inf) */
55   extern int      backfill(), rcalfill(); /* fill functions */
56 < int     (*deffill)() = backfill;        /* selected fill function */
56 > int     (*fillfunc)() = backfill;       /* selected fill function */
57   COLR    backcolr = BLKCOLR;             /* background color */
58   double  backz = 0.0;                    /* background z value */
59   int     normdist = 1;                   /* normalized distance? */
# Line 56 | Line 61 | double ourexp = -1;                    /* output picture exposure */
61  
62   VIEW    theirview = STDVIEW;            /* input view */
63   int     gotview;                        /* got input view? */
64 < int     thresolu, tvresolu;             /* input resolution */
64 > int     wrongformat = 0;                /* input in another format? */
65 > RESOLU  tresolu;                        /* input resolution */
66   double  theirexp;                       /* input picture exposure */
67   double  theirs2ours[4][4];              /* transformation matrix */
68 + int     hasmatrix = 0;                  /* has transformation matrix */
69  
70   int     childpid = -1;                  /* id of fill process */
71   FILE    *psend, *precv;                 /* pipes to/from fill calculation */
# Line 71 | Line 78 | int    argc;
78   char    *argv[];
79   {
80   #define check(olen,narg)        if (argv[i][olen] || narg >= argc-i) goto badopt
74        extern double   atof();
81          int     gotvfile = 0;
82          char    *zfile = NULL;
83          char    *err;
# Line 98 | Line 104 | char   *argv[];
104                          switch (argv[i][2]) {
105                          case '0':                               /* none */
106                                  check(3,0);
107 <                                fill = 0;
107 >                                fillo = 0;
108                                  break;
109                          case 'f':                               /* foreground */
110                                  check(3,0);
111 <                                fill = F_FORE;
111 >                                fillo = F_FORE;
112                                  break;
113                          case 'b':                               /* background */
114                                  check(3,0);
115 <                                fill = F_BACK;
115 >                                fillo = F_BACK;
116                                  break;
117                          case 'a':                               /* all */
118                                  check(3,0);
119 <                                fill = F_FORE|F_BACK;
119 >                                fillo = F_FORE|F_BACK;
120                                  break;
121 +                        case 's':                               /* sample */
122 +                                check(3,1);
123 +                                fillsamp = atoi(argv[++i]);
124 +                                break;
125                          case 'c':                               /* color */
126                                  check(3,3);
127 <                                deffill = backfill;
127 >                                fillfunc = backfill;
128                                  setcolr(backcolr, atof(argv[i+1]),
129                                          atof(argv[i+2]), atof(argv[i+3]));
130                                  i += 3;
131                                  break;
132                          case 'z':                               /* z value */
133                                  check(3,1);
134 <                                deffill = backfill;
134 >                                fillfunc = backfill;
135                                  backz = atof(argv[++i]);
136                                  break;
137                          case 'r':                               /* rtrace */
138                                  check(3,1);
139 <                                deffill = rcalfill;
139 >                                fillfunc = rcalfill;
140                                  calstart(RTCOM, argv[++i]);
141                                  break;
142                          default:
# Line 188 | Line 198 | char   *argv[];
198          for ( ; i < argc; i += 2)
199                  addpicture(argv[i], argv[i+1]);
200                                                          /* fill in spaces */
201 <        if (fill&F_BACK)
202 <                backpicture();
201 >        if (fillo&F_BACK)
202 >                backpicture(fillfunc, fillsamp);
203          else
204 <                fillpicture();
204 >                fillpicture(fillfunc);
205                                                          /* close calculation */
206          caldone();
207                                                          /* add to header */
208          printargs(argc, argv, stdout);
209          if (gotvfile) {
210 <                printf(VIEWSTR);
210 >                fputs(VIEWSTR, stdout);
211                  fprintview(&ourview, stdout);
212 <                printf("\n");
212 >                putc('\n', stdout);
213          }
214          if (pixaspect < .99 || pixaspect > 1.01)
215                  fputaspect(pixaspect, stdout);
216          if (ourexp > 0 && (ourexp < .995 || ourexp > 1.005))
217                  fputexpos(ourexp, stdout);
218 <        printf("\n");
218 >        fputformat(COLRFMT, stdout);
219 >        putc('\n', stdout);
220                                                          /* write picture */
221          writepicture();
222                                                          /* write z file */
# Line 225 | 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 +        char    fmt[32];
242  
243 <        printf("\t%s", s);
243 >        if (isformat(s)) {
244 >                formatval(fmt, s);
245 >                wrongformat = strcmp(fmt, COLRFMT);
246 >                return;
247 >        }
248 >        putc('\t', stdout);
249 >        fputs(s, stdout);
250  
251          if (isexpos(s)) {
252                  theirexp *= exposval(s);
# Line 246 | Line 264 | char   *s;
264   addpicture(pfile, zspec)                /* add picture to output */
265   char    *pfile, *zspec;
266   {
249        extern double   atof();
267          FILE    *pfp;
268          int     zfd;
269          char    *err;
# Line 263 | Line 280 | char   *pfile, *zspec;
280          theirexp = 1.0;
281          gotview = 0;
282          printf("%s:\n", pfile);
283 <        getheader(pfp, headline);
284 <        if (!gotview || fgetresolu(&thresolu, &tvresolu, pfp)
285 <                        != (YMAJOR|YDECR)) {
269 <                fprintf(stderr, "%s: picture view error\n", pfile);
283 >        getheader(pfp, headline, NULL);
284 >        if (wrongformat || !gotview || !fgetsresolu(&tresolu, pfp)) {
285 >                fprintf(stderr, "%s: picture format error\n", pfile);
286                  exit(1);
287          }
288          if (ourexp <= 0)
# Line 278 | Line 294 | char   *pfile, *zspec;
294                  exit(1);
295          }
296                                          /* compute transformation */
297 <        pixform(theirs2ours, &theirview, &ourview);
297 >        hasmatrix = pixform(theirs2ours, &theirview, &ourview);
298                                          /* allocate scanlines */
299 <        scanin = (COLR *)malloc(thresolu*sizeof(COLR));
300 <        zin = (float *)malloc(thresolu*sizeof(float));
301 <        plast = (struct position *)calloc(thresolu, sizeof(struct position));
299 >        scanin = (COLR *)malloc(scanlen(&tresolu)*sizeof(COLR));
300 >        zin = (float *)malloc(scanlen(&tresolu)*sizeof(float));
301 >        plast = (struct position *)calloc(scanlen(&tresolu),
302 >                        sizeof(struct position));
303          if (scanin == NULL || zin == NULL || plast == NULL)
304                  syserror();
305                                          /* get z specification or file */
# Line 293 | Line 310 | char   *pfile, *zspec;
310                          perror(zspec);
311                          exit(1);
312                  }
313 <                for (x = 0; x < thresolu; x++)
313 >                for (x = scanlen(&tresolu); x-- > 0; )
314                          zin[x] = zvalue;
315          }
316                                          /* load image */
317 <        for (y = tvresolu-1; y >= 0; y--) {
318 <                if (freadcolrs(scanin, thresolu, pfp) < 0) {
317 >        for (y = 0; y < numscans(&tresolu); y++) {
318 >                if (freadcolrs(scanin, scanlen(&tresolu), pfp) < 0) {
319                          fprintf(stderr, "%s: read error\n", pfile);
320                          exit(1);
321                  }
322 <                if (zfd != -1 && read(zfd,(char *)zin,thresolu*sizeof(float))
323 <                                < thresolu*sizeof(float)) {
322 >                if (zfd != -1 && read(zfd,(char *)zin,
323 >                                scanlen(&tresolu)*sizeof(float))
324 >                                < scanlen(&tresolu)*sizeof(float)) {
325                          fprintf(stderr, "%s: read error\n", zspec);
326                          exit(1);
327                  }
# Line 325 | Line 343 | register VIEW  *vw1, *vw2;
343   {
344          double  m4t[4][4];
345  
346 +        if (vw1->type != VT_PER && vw1->type != VT_PAR)
347 +                return(0);
348 +        if (vw2->type != VT_PER && vw2->type != VT_PAR)
349 +                return(0);
350          setident4(xfmat);
351          xfmat[0][0] = vw1->hvec[0];
352          xfmat[0][1] = vw1->hvec[1];
# Line 352 | Line 374 | register VIEW  *vw1, *vw2;
374          m4t[2][2] = vw2->vdir[2];
375          m4t[3][2] = -DOT(vw2->vp,vw2->vdir);
376          multmat4(xfmat, xfmat, m4t);
377 +        return(1);
378   }
379  
380  
# Line 362 | Line 385 | float  *zline;
385   struct position *lasty;         /* input/output */
386   {
387          extern double   sqrt();
388 <        double  pos[3];
388 >        FVECT   pos;
389          struct position lastx, newpos;
390          register int    x;
391  
392          lastx.z = 0;
393 <        for (x = thresolu-1; x >= 0; x--) {
394 <                pos[0] = (x+.5)/thresolu + theirview.hoff - .5;
372 <                pos[1] = (y+.5)/tvresolu + theirview.voff - .5;
393 >        for (x = scanlen(&tresolu); x-- > 0; ) {
394 >                pix2loc(pos, &tresolu, x, y);
395                  pos[2] = zline[x];
396 <                if (theirview.type == VT_PER) {
375 <                        if (normdist)   /* adjust for eye-ray distance */
376 <                                pos[2] /= sqrt( 1.
377 <                                        + pos[0]*pos[0]*theirview.hn2
378 <                                        + pos[1]*pos[1]*theirview.vn2 );
379 <                        pos[0] *= pos[2];
380 <                        pos[1] *= pos[2];
381 <                }
382 <                multp3(pos, pos, theirs2ours);
383 <                if (pos[2] <= 0) {
396 >                if (movepixel(pos) < 0) {
397                          lasty[x].z = lastx.z = 0;       /* mark invalid */
398                          continue;
399                  }
387                if (ourview.type == VT_PER) {
388                        pos[0] /= pos[2];
389                        pos[1] /= pos[2];
390                }
391                pos[0] += .5 - ourview.hoff;
392                pos[1] += .5 - ourview.voff;
400                  newpos.x = pos[0] * hresolu;
401                  newpos.y = pos[1] * vresolu;
402                  newpos.z = zline[x];
# Line 419 | Line 426 | double z;
426          register int    x, y;                   /* final position */
427  
428                                          /* compute vector p0p1 */
429 <        if (fill&F_FORE && ABS(p1->z-p0->z) <= zt) {
429 >        if (fillo&F_FORE && ABS(p1->z-p0->z) <= zt) {
430                  s1x = p1->x - p0->x;
431                  s1y = p1->y - p0->y;
432                  l1 = ABS(s1x);
# Line 430 | Line 437 | double z;
437                  p1isy = -1;
438          }
439                                          /* compute vector p0p2 */
440 <        if (fill&F_FORE && ABS(p2->z-p0->z) <= zt) {
440 >        if (fillo&F_FORE && ABS(p2->z-p0->z) <= zt) {
441                  s2x = p2->x - p0->x;
442                  s2y = p2->y - p0->y;
443                  if (p1isy == 1)
# Line 448 | Line 455 | double z;
455                  y1 = p0->y + c1*s1y/l1;
456                  for (c2 = l2; c2-- > 0; ) {
457                          x = x1 + c2*s2x/l2;
458 +                        if (x < 0 || x >= hresolu)
459 +                                continue;
460                          y = y1 + c2*s2y/l2;
461 +                        if (y < 0 || y >= vresolu)
462 +                                continue;
463                          if (zscan(y)[x] <= 0 || zscan(y)[x]-z
464                                                  > zeps*zscan(y)[x]) {
465                                  zscan(y)[x] = z;
# Line 459 | Line 470 | double z;
470   }
471  
472  
473 < backpicture()                           /* background fill algorithm */
473 > movepixel(pos)                          /* reposition image point */
474 > FVECT   pos;
475   {
476 +        FVECT   pt, direc;
477 +        
478 +        if (pos[2] <= 0)                /* empty pixel */
479 +                return(-1);
480 +        if (hasmatrix) {
481 +                pos[0] += theirview.hoff - .5;
482 +                pos[1] += theirview.voff - .5;
483 +                if (theirview.type == VT_PER) {
484 +                        if (normdist)   /* adjust for eye-ray distance */
485 +                                pos[2] /= sqrt( 1.
486 +                                        + pos[0]*pos[0]*theirview.hn2
487 +                                        + pos[1]*pos[1]*theirview.vn2 );
488 +                        pos[0] *= pos[2];
489 +                        pos[1] *= pos[2];
490 +                }
491 +                multp3(pos, pos, theirs2ours);
492 +                if (pos[2] <= 0)
493 +                        return(-1);
494 +                if (ourview.type == VT_PER) {
495 +                        pos[0] /= pos[2];
496 +                        pos[1] /= pos[2];
497 +                }
498 +                pos[0] += .5 - ourview.hoff;
499 +                pos[1] += .5 - ourview.voff;
500 +                return(0);
501 +        }
502 +        if (viewray(pt, direc, &theirview, pos[0], pos[1]) < 0)
503 +                return(-1);
504 +        pt[0] += direc[0]*pos[2];
505 +        pt[1] += direc[1]*pos[2];
506 +        pt[2] += direc[2]*pos[2];
507 +        viewloc(pos, &ourview, pt);
508 +        if (pos[2] <= 0)
509 +                return(-1);
510 +        return(0);
511 + }
512 +
513 +
514 + backpicture(fill, samp)                 /* background fill algorithm */
515 + int     (*fill)();
516 + int     samp;
517 + {
518          int     *yback, xback;
519          int     y;
466        COLR    pfill;
520          register int    x, i;
521                                                          /* get back buffer */
522          yback = (int *)malloc(hresolu*sizeof(int));
# Line 510 | Line 563 | backpicture()                          /* background fill algorithm */
563                                                  xback = x-1;
564                                  }
565                                  /*
566 <                                 * Check to see if we have no background for
567 <                                 * this pixel.  If not, use background color.
566 >                                 * If we have no background for this pixel,
567 >                                 * use the given fill function.
568                                   */
569 <                                if (xback < 0 && yback[x] < 0) {
570 <                                        (*deffill)(x,y);
518 <                                        continue;
519 <                                }
569 >                                if (xback < 0 && yback[x] < 0)
570 >                                        goto fillit;
571                                  /*
572                                   * Compare, and use the background that is
573                                   * farther, unless one of them is next to us.
574 +                                 * If the background is too distant, call
575 +                                 * the fill function.
576                                   */
577                                  if ( yback[x] < 0
578                                          || (xback >= 0 && ABS(x-xback) <= 1)
579                                          || ( ABS(y-yback[x]) > 1
580                                                  && zscan(yback[x])[x]
581                                                  < zscan(y)[xback] ) ) {
582 +                                        if (samp > 0 && ABS(x-xback) >= samp)
583 +                                                goto fillit;
584                                          copycolr(pscan(y)[x],pscan(y)[xback]);
585                                          zscan(y)[x] = zscan(y)[xback];
586                                  } else {
587 +                                        if (samp > 0 && ABS(y-yback[x]) > samp)
588 +                                                goto fillit;
589                                          copycolr(pscan(y)[x],pscan(yback[x])[x]);
590                                          zscan(y)[x] = zscan(yback[x])[x];
591                                  }
592 +                                continue;
593 +                        fillit:
594 +                                (*fill)(x,y);
595 +                                if (fill == rcalfill) {         /* use it */
596 +                                        clearqueue();
597 +                                        xback = x;
598 +                                        yback[x] = y;
599 +                                }
600                          } else {                                /* full pixel */
601                                  yback[x] = -2;
602                                  xback = -2;
# Line 541 | Line 606 | backpicture()                          /* background fill algorithm */
606   }
607  
608  
609 < fillpicture()                           /* paint in empty pixels with default */
609 > fillpicture(fill)               /* paint in empty pixels using fill */
610 > int     (*fill)();
611   {
612          register int    x, y;
613  
614          for (y = 0; y < vresolu; y++)
615                  for (x = 0; x < hresolu; x++)
616                          if (zscan(y)[x] <= 0)
617 <                                (*deffill)(x,y);
617 >                                (*fill)(x,y);
618   }
619  
620  
# Line 556 | Line 622 | writepicture()                         /* write out picture */
622   {
623          int     y;
624  
625 <        fputresolu(YMAJOR|YDECR, hresolu, vresolu, stdout);
625 >        fprtresolu(hresolu, vresolu, stdout);
626          for (y = vresolu-1; y >= 0; y--)
627                  if (fwritecolrs(pscan(y), hresolu, stdout) < 0)
628                          syserror();
# Line 583 | Line 649 | char   *fname;
649                  if (donorm) {
650                          double  vx, yzn2;
651                          register int    x;
652 <                        yzn2 = y - .5*(vresolu-1);
652 >                        yzn2 = (y+.5)/vresolu + ourview.voff - .5;
653                          yzn2 = 1. + yzn2*yzn2*ourview.vn2;
654                          for (x = 0; x < hresolu; x++) {
655 <                                vx = x - .5*(hresolu-1);
655 >                                vx = (x+.5)/hresolu + ourview.hoff - .5;
656                                  zout[x] = zscan(y)[x]
657                                          * sqrt(vx*vx*ourview.hn2 + yzn2);
658                          }
# Line 635 | Line 701 | char   *prog, *args;
701                  fprintf(stderr, "%s: too many calculations\n", progname);
702                  exit(1);
703          }
704 <        sprintf(combuf, prog, PACKSIZ, args);
704 >        sprintf(combuf, prog, args);
705          if (pipe(p0) < 0 || pipe(p1) < 0)
706                  syserror();
707          if ((childpid = vfork()) == 0) {        /* fork calculation */
# Line 671 | Line 737 | caldone()                              /* done with calculation */
737  
738          if (childpid == -1)
739                  return;
740 <        clearqueue(1);
740 >        clearqueue();
741 >        fclose(psend);
742 >        fclose(precv);
743          while ((pid = wait(0)) != -1 && pid != childpid)
744                  ;
745          childpid = -1;
# Line 682 | Line 750 | rcalfill(x, y)                         /* fill with ray-calculated pixel */
750   int     x, y;
751   {
752          if (queuesiz >= PACKSIZ)        /* flush queue if needed */
753 <                clearqueue(0);
753 >                clearqueue();
754                                          /* add position to queue */
755          queue[queuesiz][0] = x;
756          queue[queuesiz][1] = y;
# Line 690 | Line 758 | int    x, y;
758   }
759  
760  
761 < clearqueue(done)                        /* process queue */
694 < int     done;
761 > clearqueue()                            /* process queue */
762   {
763          FVECT   orig, dir;
764          float   fbuf[6];
# Line 705 | Line 772 | int    done;
772                  fbuf[3] = dir[0]; fbuf[4] = dir[1]; fbuf[5] = dir[2];
773                  fwrite((char *)fbuf, sizeof(float), 6, psend);
774          }
775 <        if ((done ? fclose(psend) : fflush(psend)) == EOF)
775 >                                        /* flush output and get results */
776 >        fbuf[3] = fbuf[4] = fbuf[5] = 0.0;              /* mark */
777 >        fwrite((char *)fbuf, sizeof(float), 6, psend);
778 >        if (fflush(psend) == EOF)
779                  syserror();
780          for (i = 0; i < queuesiz; i++) {
781                  if (fread((char *)fbuf, sizeof(float), 4, precv) < 4) {
# Line 722 | Line 792 | int    done;
792                                  fbuf[0], fbuf[1], fbuf[2]);
793                  zscan(queue[i][1])[queue[i][0]] = fbuf[3];
794          }
725        if (done)
726                fclose(precv);
795          queuesiz = 0;
796   }
797  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines