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.12 by greg, Thu Jan 4 14:25:56 1990 UTC vs.
Revision 1.16 by greg, Sat Jan 6 22:27:10 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 */
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 + struct position {int x,y; float z;};
30 +
31   VIEW    ourview = STDVIEW(512);         /* desired view */
32  
33   double  zeps = .02;                     /* allowed z epsilon */
# Line 31 | Line 37 | float  *ourzbuf;                       /* corresponding z-buffer */
37  
38   char    *progname;
39  
34 VIEW    theirview = STDVIEW(512);       /* input view */
35 int     gotview;                        /* got input view? */
36
40   int     fill = F_FORE|F_BACK;           /* selected fill algorithm */
41 < extern int      backfill();             /* fill functions */
41 > extern int      backfill(), rcalfill(); /* fill functions */
42   int     (*deffill)() = backfill;        /* selected fill function */
43   COLR    backcolr = BLKCOLR;             /* background color */
44 + double  backz = 0.0;                    /* background z value */
45 + int     normdist = 1;                   /* normalized distance? */
46 + double  ourexp = -1;                    /* output picture exposure */
47  
48 + VIEW    theirview = STDVIEW(512);       /* input view */
49 + int     gotview;                        /* got input view? */
50   double  theirs2ours[4][4];              /* transformation matrix */
51 < int     normdist = 1;                   /* normalized distance? */
51 > double  theirexp;                       /* input picture exposure */
52  
53 + int     childpid = -1;                  /* id of fill process */
54 + FILE    *psend, *precv;                 /* pipes to/from fill calculation */
55 + int     queue[PACKSIZ][2];              /* pending pixels */
56 + int     queuesiz;                       /* number of pixels pending */
57  
58 +
59   main(argc, argv)                        /* interpolate pictures */
60   int     argc;
61   char    *argv[];
# Line 91 | Line 104 | char   *argv[];
104                                          atof(argv[i+2]), atof(argv[i+3]));
105                                  i += 3;
106                                  break;
107 +                        case 'z':                               /* z value */
108 +                                check(3,1);
109 +                                deffill = backfill;
110 +                                backz = atof(argv[++i]);
111 +                                break;
112 +                        case 'r':                               /* rtrace */
113 +                                check(3,1);
114 +                                deffill = rcalfill;
115 +                                calstart(RTCOM, argv[++i]);
116 +                                break;
117                          default:
118                                  goto badopt;
119                          }
# Line 162 | Line 185 | char   *argv[];
185                          goto userr;
186                  }
187                                                  /* check arguments */
188 <        if (argc-i < 2 || (argc-i)%2)
188 >        if ((argc-i)%2)
189                  goto userr;
190                                                  /* set view */
191          if (err = setview(&ourview)) {
# Line 172 | Line 195 | char   *argv[];
195                                                  /* allocate frame */
196          ourpict = (COLR *)malloc(ourview.hresolu*ourview.vresolu*sizeof(COLR));
197          ourzbuf = (float *)calloc(ourview.hresolu*ourview.vresolu,sizeof(float));
198 <        if (ourpict == NULL || ourzbuf == NULL) {
199 <                perror(progname);
177 <                exit(1);
178 <        }
198 >        if (ourpict == NULL || ourzbuf == NULL)
199 >                syserror();
200                                                          /* get input */
201          for ( ; i < argc; i += 2)
202                  addpicture(argv[i], argv[i+1]);
# Line 184 | Line 205 | char   *argv[];
205                  backpicture();
206          else
207                  fillpicture();
208 +                                                        /* close calculation */
209 +        caldone();
210                                                          /* add to header */
211          printargs(argc, argv, stdout);
212          if (gotvfile) {
# Line 191 | Line 214 | char   *argv[];
214                  fprintview(&ourview, stdout);
215                  printf("\n");
216          }
217 +        if (ourexp > 0 && ourexp != 1.0)
218 +                fputexpos(ourexp, stdout);
219          printf("\n");
220                                                          /* write picture */
221          writepicture();
# Line 216 | Line 241 | char   *s;
241  
242          printf("\t%s", s);
243  
244 +        if (isexpos(s)) {
245 +                theirexp *= exposval(s);
246 +                return;
247 +        }
248          for (an = altname; *an != NULL; an++)
249                  if (!strncmp(*an, s, strlen(*an))) {
250                          if (sscanview(&theirview, s+strlen(*an)) == 0)
# Line 232 | Line 261 | char   *pfile, *zspec;
261          FILE    *pfp, *zfp;
262          char    *err;
263          COLR    *scanin;
264 <        float   *zin, *zlast;
265 <        int     *plast;
264 >        float   *zin;
265 >        struct position *plast;
266          int     y;
267                                          /* open picture file */
268          if ((pfp = fopen(pfile, "r")) == NULL) {
269                  perror(pfile);
270                  exit(1);
271          }
272 <                                        /* get header and view */
273 <        printf("%s:\n", pfile);
272 >                                        /* get header with exposure and view */
273 >        theirexp = 1.0;
274          gotview = 0;
275 +        printf("%s:\n", pfile);
276          getheader(pfp, headline);
277          if (!gotview || fgetresolu(&theirview.hresolu, &theirview.vresolu, pfp)
278                          != (YMAJOR|YDECR)) {
279                  fprintf(stderr, "%s: picture view error\n", pfile);
280                  exit(1);
281          }
282 +        if (ourexp <= 0)
283 +                ourexp = theirexp;
284 +        else if (ABS(theirexp-ourexp) > .01*ourexp)
285 +                fprintf(stderr, "%s: different exposure (warning)\n", pfile);
286          if (err = setview(&theirview)) {
287                  fprintf(stderr, "%s: %s\n", pfile, err);
288                  exit(1);
# Line 258 | Line 292 | char   *pfile, *zspec;
292                                          /* allocate scanlines */
293          scanin = (COLR *)malloc(theirview.hresolu*sizeof(COLR));
294          zin = (float *)malloc(theirview.hresolu*sizeof(float));
295 <        plast = (int *)calloc(theirview.hresolu, sizeof(int));
296 <        zlast = (float *)calloc(theirview.hresolu, sizeof(float));
297 <        if (scanin == NULL || zin == NULL || plast == NULL || zlast == NULL) {
298 <                perror(progname);
265 <                exit(1);
266 <        }
295 >        plast = (struct position *)calloc(theirview.hresolu,
296 >                                                sizeof(struct position));
297 >        if (scanin == NULL || zin == NULL || plast == NULL)
298 >                syserror();
299                                          /* get z specification or file */
300          if ((zfp = fopen(zspec, "r")) == NULL) {
301                  double  zvalue;
# Line 287 | Line 319 | char   *pfile, *zspec;
319                          fprintf(stderr, "%s: read error\n", zspec);
320                          exit(1);
321                  }
322 <                addscanline(y, scanin, zin, plast, zlast);
322 >                addscanline(y, scanin, zin, plast);
323          }
324                                          /* clean up */
325          free((char *)scanin);
326          free((char *)zin);
327          free((char *)plast);
296        free((char *)zlast);
328          fclose(pfp);
329          if (zfp != NULL)
330                  fclose(zfp);
# Line 336 | Line 367 | register VIEW  *vw1, *vw2;
367   }
368  
369  
370 < addscanline(y, pline, zline, lasty, lastyz)     /* add scanline to output */
370 > addscanline(y, pline, zline, lasty)     /* add scanline to output */
371   int     y;
372   COLR    *pline;
373   float   *zline;
374 < int     *lasty;                 /* input/output */
344 < float   *lastyz;                /* input/output */
374 > struct position *lasty;         /* input/output */
375   {
376          extern double   sqrt();
377          double  pos[3];
378 <        int     lastx = 0;
349 <        double  lastxz = 0;
350 <        double  zt;
351 <        int     xpos, ypos;
378 >        struct position lastx, newpos;
379          register int    x;
380  
381          for (x = theirview.hresolu-1; x >= 0; x--) {
# Line 364 | Line 391 | float  *lastyz;                /* input/output */
391                          pos[1] *= pos[2];
392                  }
393                  multp3(pos, pos, theirs2ours);
394 <                if (pos[2] <= 0)
394 >                if (pos[2] <= 0) {
395 >                        lasty[x].z = lastx.z = 0;       /* mark invalid */
396                          continue;
397 +                }
398                  if (ourview.type == VT_PER) {
399                          pos[0] /= pos[2];
400                          pos[1] /= pos[2];
401                  }
402                  pos[0] += .5*ourview.hresolu;
403                  pos[1] += .5*ourview.vresolu;
404 <                if (pos[0] < 0 || (xpos = pos[0]) >= ourview.hresolu
405 <                        || pos[1] < 0 || (ypos = pos[1]) >= ourview.vresolu)
406 <                        continue;
404 >                newpos.x = pos[0];
405 >                newpos.y = pos[1];
406 >                newpos.z = zline[x];
407                                          /* add pixel to our image */
408 <                zt = 2.*zeps*zline[x];
409 <                addpixel(xpos, ypos,
410 <                        (fill&F_FORE && ABS(zline[x]-lastxz) <= zt)
411 <                                ? lastx - xpos : 1,
412 <                        (fill&F_FORE && ABS(zline[x]-lastyz[x]) <= zt)
413 <                                ? lasty[x] - ypos : 1,
414 <                        pline[x], pos[2]);
415 <                lastx = xpos;
387 <                lasty[x] = ypos;
388 <                lastxz = lastyz[x] = zline[x];
408 >                if (pos[0] >= 0 && newpos.x < ourview.hresolu
409 >                                && pos[1] >= 0 && newpos.y < ourview.vresolu) {
410 >                        addpixel(&newpos, &lastx, &lasty[x], pline[x], pos[2]);
411 >                        lasty[x].x = lastx.x = newpos.x;
412 >                        lasty[x].y = lastx.y = newpos.y;
413 >                        lasty[x].z = lastx.z = newpos.z;
414 >                } else
415 >                        lasty[x].z = lastx.z = 0;       /* mark invalid */
416          }
417   }
418  
419  
420 < addpixel(xstart, ystart, width, height, pix, z) /* fill in area for pixel */
421 < int     xstart, ystart;
395 < int     width, height;
420 > addpixel(p0, p1, p2, pix, z)            /* fill in pixel parallelogram */
421 > struct position *p0, *p1, *p2;
422   COLR    pix;
423   double  z;
424   {
425 <        register int    x, y;
426 <                                        /* make width and height positive */
427 <        if (width < 0) {
428 <                width = -width;
429 <                xstart = xstart-width+1;
430 <        } else if (width == 0)
431 <                width = 1;
432 <        if (height < 0) {
433 <                height = -height;
434 <                ystart = ystart-height+1;
435 <        } else if (height == 0)
436 <                height = 1;
437 <                                        /* fill pixel(s) within rectangle */
438 <        for (y = ystart; y < ystart+height; y++)
439 <                for (x = xstart; x < xstart+width; x++)
440 <                        if (zscan(y)[x] <= 0
441 <                                        || zscan(y)[x]-z > zeps*zscan(y)[x]) {
425 >        double  zt = 2.*zeps*p0->z;             /* threshold */
426 >        int     s1x, s1y, s2x, s2y;             /* step sizes */
427 >        int     l1, l2, c1, c2;                 /* side lengths and counters */
428 >        int     p1isy;                          /* p0p1 along y? */
429 >        int     x1, y1;                         /* p1 position */
430 >        register int    x, y;                   /* final position */
431 >
432 >                                        /* compute vector p0p1 */
433 >        if (fill&F_FORE && ABS(p1->z-p0->z) <= zt) {
434 >                s1x = p1->x - p0->x;
435 >                s1y = p1->y - p0->y;
436 >                l1 = ABS(s1x);
437 >                if (p1isy = (ABS(s1y) > l1))
438 >                        l1 = ABS(s1y);
439 >        } else {
440 >                l1 = s1x = s1y = 1;
441 >                p1isy = -1;
442 >        }
443 >                                        /* compute vector p0p2 */
444 >        if (fill&F_FORE && ABS(p2->z-p0->z) <= zt) {
445 >                s2x = p2->x - p0->x;
446 >                s2y = p2->y - p0->y;
447 >                if (p1isy == 1)
448 >                        l2 = ABS(s2x);
449 >                else {
450 >                        l2 = ABS(s2y);
451 >                        if (p1isy != 0 && ABS(s2x) > l2)
452 >                                l2 = ABS(s2x);
453 >                }
454 >        } else
455 >                l2 = s2x = s2y = 1;
456 >                                        /* fill the parallelogram */
457 >        for (c1 = l1; c1-- > 0; ) {
458 >                x1 = p0->x + c1*s1x/l1;
459 >                y1 = p0->y + c1*s1y/l1;
460 >                for (c2 = l2; c2-- > 0; ) {
461 >                        x = x1 + c2*s2x/l2;
462 >                        y = y1 + c2*s2y/l2;
463 >                        if (zscan(y)[x] <= 0 || zscan(y)[x]-z
464 >                                                > zeps*zscan(y)[x]) {
465                                  zscan(y)[x] = z;
466                                  copycolr(pscan(y)[x], pix);
467                          }
468 +                }
469 +        }
470   }
471  
472  
# Line 427 | Line 478 | backpicture()                          /* background fill algorithm */
478          register int    x, i;
479                                                          /* get back buffer */
480          yback = (int *)malloc(ourview.hresolu*sizeof(int));
481 <        if (yback == NULL) {
482 <                perror(progname);
432 <                return;
433 <        }
481 >        if (yback == NULL)
482 >                syserror();
483          for (x = 0; x < ourview.hresolu; x++)
484                  yback[x] = -2;
485          /*
# Line 487 | Line 536 | backpicture()                          /* background fill algorithm */
536                                          || (xback >= 0 && ABS(x-xback) <= 1)
537                                          || ( ABS(y-yback[x]) > 1
538                                                  && zscan(yback[x])[x]
539 <                                                < zscan(y)[xback] ) )
539 >                                                < zscan(y)[xback] ) ) {
540                                          copycolr(pscan(y)[x],pscan(y)[xback]);
541 <                                else
541 >                                        zscan(y)[x] = zscan(y)[xback];
542 >                                } else {
543                                          copycolr(pscan(y)[x],pscan(yback[x])[x]);
544 +                                        zscan(y)[x] = zscan(yback[x])[x];
545 +                                }
546                          } else {                                /* full pixel */
547                                  yback[x] = -2;
548                                  xback = -2;
# Line 517 | Line 569 | writepicture()                         /* write out picture */
569  
570          fputresolu(YMAJOR|YDECR, ourview.hresolu, ourview.vresolu, stdout);
571          for (y = ourview.vresolu-1; y >= 0; y--)
572 <                if (fwritecolrs(pscan(y), ourview.hresolu, stdout) < 0) {
573 <                        perror(progname);
522 <                        exit(1);
523 <                }
572 >                if (fwritecolrs(pscan(y), ourview.hresolu, stdout) < 0)
573 >                        syserror();
574   }
575  
576  
# Line 538 | Line 588 | char   *fname;
588                  exit(1);
589          }
590          if (donorm
591 <        && (zout = (float *)malloc(ourview.hresolu*sizeof(float))) == NULL) {
592 <                perror(progname);
543 <                exit(1);
544 <        }
591 >        && (zout = (float *)malloc(ourview.hresolu*sizeof(float))) == NULL)
592 >                syserror();
593          for (y = ourview.vresolu-1; y >= 0; y--) {
594                  if (donorm) {
595                          double  vx, yzn2;
# Line 584 | Line 632 | int    x, y;
632          register BYTE   *dest = pscan(y)[x];
633  
634          copycolr(dest, backcolr);
635 +        zscan(y)[x] = backz;
636 + }
637 +
638 +
639 + calstart(prog, args)                    /* start fill calculation */
640 + char    *prog, *args;
641 + {
642 +        char    combuf[512];
643 +        int     p0[2], p1[2];
644 +
645 +        if (childpid != -1) {
646 +                fprintf(stderr, "%s: too many calculations\n", progname);
647 +                exit(1);
648 +        }
649 +        sprintf(combuf, prog, PACKSIZ, args);
650 +        if (pipe(p0) < 0 || pipe(p1) < 0)
651 +                syserror();
652 +        if ((childpid = vfork()) == 0) {        /* fork calculation */
653 +                close(p0[1]);
654 +                close(p1[0]);
655 +                if (p0[0] != 0) {
656 +                        dup2(p0[0], 0);
657 +                        close(p0[0]);
658 +                }
659 +                if (p1[1] != 1) {
660 +                        dup2(p1[1], 1);
661 +                        close(p1[1]);
662 +                }
663 +                execl("/bin/sh", "sh", "-c", combuf, 0);
664 +                perror("/bin/sh");
665 +                _exit(127);
666 +        }
667 +        if (childpid == -1)
668 +                syserror();
669 +        close(p0[0]);
670 +        close(p1[1]);
671 +        if ((psend = fdopen(p0[1], "w")) == NULL)
672 +                syserror();
673 +        if ((precv = fdopen(p1[0], "r")) == NULL)
674 +                syserror();
675 +        queuesiz = 0;
676 + }
677 +
678 +
679 + caldone()                               /* done with calculation */
680 + {
681 +        int     pid;
682 +
683 +        if (childpid == -1)
684 +                return;
685 +        if (fclose(psend) == EOF)
686 +                syserror();
687 +        clearqueue();
688 +        fclose(precv);
689 +        while ((pid = wait(0)) != -1 && pid != childpid)
690 +                ;
691 +        childpid = -1;
692 + }
693 +
694 +
695 + rcalfill(x, y)                          /* fill with ray-calculated pixel */
696 + int     x, y;
697 + {
698 +        FVECT   orig, dir;
699 +        float   outbuf[6];
700 +
701 +        if (queuesiz >= PACKSIZ) {      /* flush queue */
702 +                if (fflush(psend) == EOF)
703 +                        syserror();
704 +                clearqueue();
705 +        }
706 +                                        /* send new ray */
707 +        rayview(orig, dir, &ourview, x+.5, y+.5);
708 +        outbuf[0] = orig[0]; outbuf[1] = orig[1]; outbuf[2] = orig[2];
709 +        outbuf[3] = dir[0]; outbuf[4] = dir[1]; outbuf[5] = dir[2];
710 +        if (fwrite(outbuf, sizeof(float), 6, psend) < 6)
711 +                syserror();
712 +                                        /* remember it */
713 +        queue[queuesiz][0] = x;
714 +        queue[queuesiz][1] = y;
715 +        queuesiz++;
716 + }
717 +
718 +
719 + clearqueue()                            /* get results from queue */
720 + {
721 +        float   inbuf[4];
722 +        register int    i;
723 +
724 +        for (i = 0; i < queuesiz; i++) {
725 +                if (fread(inbuf, sizeof(float), 4, precv) < 4) {
726 +                        fprintf(stderr, "%s: read error in clearqueue\n",
727 +                                        progname);
728 +                        exit(1);
729 +                }
730 +                if (ourexp > 0 && ourexp != 1.0) {
731 +                        inbuf[0] *= ourexp;
732 +                        inbuf[1] *= ourexp;
733 +                        inbuf[2] *= ourexp;
734 +                }
735 +                setcolr(pscan(queue[i][1])[queue[i][0]],
736 +                                inbuf[0], inbuf[1], inbuf[2]);
737 +                zscan(queue[i][1])[queue[i][0]] = inbuf[3];
738 +        }
739 +        queuesiz = 0;
740 + }
741 +
742 +
743 + syserror()                      /* report error and exit */
744 + {
745 +        perror(progname);
746 +        exit(1);
747   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines