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 2.9 by greg, Fri Oct 2 16:24:22 1992 UTC vs.
Revision 2.18 by greg, Fri Dec 23 19:15:48 1994 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1991 Regents of the University of California */
1 > /* Copyright (c) 1994 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 20 | Line 20 | static char SCCSid[] = "$SunId$ LBL";
20  
21   #include "resolu.h"
22  
23 #ifndef BSD
24 #define vfork           fork
25 #endif
26
23   #define pscan(y)        (ourpict+(y)*hresolu)
24   #define zscan(y)        (ourzbuf+(y)*hresolu)
25  
# Line 38 | Line 34 | static char SCCSid[] = "$SunId$ LBL";
34  
35   struct position {int x,y; float z;};
36  
37 + #define NSTEPS          64              /* number steps in overlap prescan */
38 + #define MINSTEP         4               /* minimum worthwhile preview step */
39 +
40 + struct bound {int min,max;};
41 +
42   VIEW    ourview = STDVIEW;              /* desired view */
43   int     hresolu = 512;                  /* horizontal resolution */
44   int     vresolu = 512;                  /* vertical resolution */
# Line 169 | Line 170 | char   *argv[];
170                                  goto badopt;
171                          check(3,"s");
172                          gotvfile = viewfile(argv[++i], &ourview, 0, 0);
173 <                        if (gotvfile < 0) {
174 <                                perror(argv[i]);
175 <                                exit(1);
175 <                        } else if (gotvfile == 0) {
173 >                        if (gotvfile < 0)
174 >                                syserror(argv[i]);
175 >                        else if (gotvfile == 0) {
176                                  fprintf(stderr, "%s: bad view file\n",
177                                                  argv[i]);
178                                  exit(1);
# Line 191 | Line 191 | char   *argv[];
191          if (fillsamp == 1)
192                  fillo &= ~F_BACK;
193                                                  /* set view */
194 <        if (err = setview(&ourview)) {
194 >        if ((err = setview(&ourview)) != NULL) {
195                  fprintf(stderr, "%s: %s\n", progname, err);
196                  exit(1);
197          }
# Line 200 | Line 200 | char   *argv[];
200          ourpict = (COLR *)bmalloc(hresolu*vresolu*sizeof(COLR));
201          ourzbuf = (float *)bmalloc(hresolu*vresolu*sizeof(float));
202          if (ourpict == NULL || ourzbuf == NULL)
203 <                syserror();
203 >                syserror(progname);
204          bzero((char *)ourzbuf, hresolu*vresolu*sizeof(float));
205 +                                                        /* new header */
206 +        newheader("RADIANCE", stdout);
207                                                          /* get input */
208          for ( ; i < argc; i += 2)
209                  addpicture(argv[i], argv[i+1]);
# Line 212 | Line 214 | char   *argv[];
214                  fillpicture(fillfunc);
215                                                          /* close calculation */
216          caldone();
217 +                                                        /* aft clipping */
218 +        clipaft();
219                                                          /* add to header */
220          printargs(argc, argv, stdout);
221          if (gotvfile) {
# Line 246 | Line 250 | char   *s;
250   {
251          char    fmt[32];
252  
253 <        if (isformat(s)) {
254 <                formatval(fmt, s);
253 >        if (isheadid(s))
254 >                return;
255 >        if (formatval(fmt, s)) {
256                  wrongformat = strcmp(fmt, COLRFMT);
257                  return;
258          }
# Line 272 | Line 277 | char   *pfile, *zspec;
277          COLR    *scanin;
278          float   *zin;
279          struct position *plast;
280 +        struct bound    *xlim, ylim;
281          int     y;
282                                          /* open picture file */
283 <        if ((pfp = fopen(pfile, "r")) == NULL) {
284 <                perror(pfile);
279 <                exit(1);
280 <        }
283 >        if ((pfp = fopen(pfile, "r")) == NULL)
284 >                syserror(pfile);
285                                          /* get header with exposure and view */
286          theirexp = 1.0;
287          gotview = 0;
# Line 297 | Line 301 | char   *pfile, *zspec;
301          }
302                                          /* compute transformation */
303          hasmatrix = pixform(theirs2ours, &theirview, &ourview);
300                                        /* allocate scanlines */
301        scanin = (COLR *)malloc(scanlen(&tresolu)*sizeof(COLR));
302        zin = (float *)malloc(scanlen(&tresolu)*sizeof(float));
303        plast = (struct position *)calloc(scanlen(&tresolu),
304                        sizeof(struct position));
305        if (scanin == NULL || zin == NULL || plast == NULL)
306                syserror();
304                                          /* get z specification or file */
305 +        zin = (float *)malloc(scanlen(&tresolu)*sizeof(float));
306 +        if (zin == NULL)
307 +                syserror(progname);
308          if ((zfd = open(zspec, O_RDONLY)) == -1) {
309                  double  zvalue;
310                  register int    x;
311 <                if (!isfloat(zspec) || (zvalue = atof(zspec)) <= 0.0) {
312 <                        perror(zspec);
313 <                        exit(1);
314 <                }
311 >                if (!isfloat(zspec) || (zvalue = atof(zspec)) <= 0.0)
312 >                        syserror(zspec);
313                  for (x = scanlen(&tresolu); x-- > 0; )
314                          zin[x] = zvalue;
315          }
316 <                                        /* load image */
317 <        for (y = 0; y < numscans(&tresolu); y++) {
316 >                                        /* compute transferrable perimeter */
317 >        xlim = (struct bound *)malloc(numscans(&tresolu)*sizeof(struct bound));
318 >        if (xlim == NULL)
319 >                syserror(progname);
320 >        if (!getperim(xlim, &ylim, zin, zfd)) { /* overlapping area? */
321 >                free((char *)zin);
322 >                free((char *)xlim);
323 >                if (zfd != -1)
324 >                        close(zfd);
325 >                fclose(pfp);
326 >                return;
327 >        }
328 >                                        /* allocate scanlines */
329 >        scanin = (COLR *)malloc(scanlen(&tresolu)*sizeof(COLR));
330 >        plast = (struct position *)calloc(scanlen(&tresolu),
331 >                        sizeof(struct position));
332 >        if (scanin == NULL | plast == NULL)
333 >                syserror(progname);
334 >                                        /* skip to starting point */
335 >        for (y = 0; y < ylim.min; y++)
336                  if (freadcolrs(scanin, scanlen(&tresolu), pfp) < 0) {
337                          fprintf(stderr, "%s: read error\n", pfile);
338                          exit(1);
339                  }
340 <                if (zfd != -1 && read(zfd,(char *)zin,
341 <                                scanlen(&tresolu)*sizeof(float))
342 <                                < scanlen(&tresolu)*sizeof(float)) {
343 <                        fprintf(stderr, "%s: read error\n", zspec);
340 >        if (zfd != -1 && lseek(zfd,
341 >                        (long)ylim.min*scanlen(&tresolu)*sizeof(float), 0) < 0)
342 >                syserror(zspec);
343 >                                        /* load image */
344 >        for (y = ylim.min; y <= ylim.max; y++) {
345 >                if (freadcolrs(scanin, scanlen(&tresolu), pfp) < 0) {
346 >                        fprintf(stderr, "%s: read error\n", pfile);
347                          exit(1);
348                  }
349 <                addscanline(y, scanin, zin, plast);
349 >                if (zfd != -1 && read(zfd, (char *)zin,
350 >                                scanlen(&tresolu)*sizeof(float))
351 >                                < scanlen(&tresolu)*sizeof(float))
352 >                        syserror(zspec);
353 >                addscanline(xlim+y, y, scanin, zin, plast);
354          }
355                                          /* clean up */
356 +        free((char *)xlim);
357          free((char *)scanin);
358          free((char *)zin);
359          free((char *)plast);
# Line 380 | Line 404 | register VIEW  *vw1, *vw2;
404   }
405  
406  
407 < addscanline(y, pline, zline, lasty)     /* add scanline to output */
407 > addscanline(xl, y, pline, zline, lasty) /* add scanline to output */
408 > struct bound    *xl;
409   int     y;
410   COLR    *pline;
411   float   *zline;
# Line 391 | Line 416 | struct position        *lasty;         /* input/output */
416          register int    x;
417  
418          lastx.z = 0;
419 <        for (x = scanlen(&tresolu); x-- > 0; ) {
419 >        for (x = xl->max; x >= xl->min; x--) {
420                  pix2loc(pos, &tresolu, x, y);
421                  pos[2] = zline[x];
422                  if (movepixel(pos) < 0) {
# Line 474 | Line 499 | double z;
499   movepixel(pos)                          /* reposition image point */
500   FVECT   pos;
501   {
502 +        double  d0, d1;
503          FVECT   pt, direc;
504          
505          if (pos[2] <= 0)                /* empty pixel */
506                  return(-1);
507 +        if (normdist && theirview.type == VT_PER) {     /* adjust distance */
508 +                d0 = pos[0] + theirview.hoff - .5;
509 +                d1 = pos[1] + theirview.voff - .5;
510 +                pos[2] /= sqrt(1. + d0*d0*theirview.hn2 + d1*d1*theirview.vn2);
511 +        }
512          if (hasmatrix) {
513                  pos[0] += theirview.hoff - .5;
514                  pos[1] += theirview.voff - .5;
515                  if (theirview.type == VT_PER) {
485                        if (normdist)   /* adjust for eye-ray distance */
486                                pos[2] /= sqrt( 1.
487                                        + pos[0]*pos[0]*theirview.hn2
488                                        + pos[1]*pos[1]*theirview.vn2 );
516                          pos[0] *= pos[2];
517                          pos[1] *= pos[2];
518                  }
# Line 500 | Line 527 | FVECT  pos;
527                  pos[1] += .5 - ourview.voff;
528                  return(0);
529          }
530 <        if (viewray(pt, direc, &theirview, pos[0], pos[1]) < 0)
530 >        if (viewray(pt, direc, &theirview, pos[0], pos[1]) < -FTINY)
531                  return(-1);
532          pt[0] += direc[0]*pos[2];
533          pt[1] += direc[1]*pos[2];
# Line 512 | Line 539 | FVECT  pos;
539   }
540  
541  
542 + getperim(xl, yl, zline, zfd)            /* compute overlapping image area */
543 + register struct bound   *xl;
544 + struct bound    *yl;
545 + float   *zline;
546 + int     zfd;
547 + {
548 +        int     step;
549 +        FVECT   pos;
550 +        register int    x, y;
551 +                                                /* set up step size */
552 +        if (scanlen(&tresolu) < numscans(&tresolu))
553 +                step = scanlen(&tresolu)/NSTEPS;
554 +        else
555 +                step = numscans(&tresolu)/NSTEPS;
556 +        if (step < MINSTEP) {                   /* not worth cropping? */
557 +                yl->min = 0;
558 +                yl->max = numscans(&tresolu) - 1;
559 +                x = scanlen(&tresolu) - 1;
560 +                for (y = numscans(&tresolu); y--; ) {
561 +                        xl[y].min = 0;
562 +                        xl[y].max = x;
563 +                }
564 +                return(1);
565 +        }
566 +        yl->min = 32000; yl->max = 0;           /* search for points on image */
567 +        for (y = step - 1; y < numscans(&tresolu); y += step) {
568 +                if (zfd != -1) {
569 +                        if (lseek(zfd, (long)y*scanlen(&tresolu)*sizeof(float),
570 +                                        0) < 0)
571 +                                syserror("lseek");
572 +                        if (read(zfd, (char *)zline,
573 +                                        scanlen(&tresolu)*sizeof(float))
574 +                                        < scanlen(&tresolu)*sizeof(float))
575 +                                syserror("read");
576 +                }
577 +                xl[y].min = 32000; xl[y].max = 0;               /* x max */
578 +                for (x = scanlen(&tresolu); (x -= step) > 0; ) {
579 +                        pix2loc(pos, &tresolu, x, y);
580 +                        pos[2] = zline[x];
581 +                        if (movepixel(pos) == 0 && pos[0] >= 0 &&
582 +                                        pos[0] < 1 && pos[1] >= 0 &&
583 +                                        pos[1] < 1) {
584 +                                xl[y].max = x + step - 1;
585 +                                xl[y].min = x - step + 1;       /* x min */
586 +                                if (xl[y].min < 0)
587 +                                        xl[y].min = 0;
588 +                                for (x = step - 1; x < xl[y].max; x += step) {
589 +                                        pix2loc(pos, &tresolu, x, y);
590 +                                        pos[2] = zline[x];
591 +                                        if (movepixel(pos) == 0 &&
592 +                                                        pos[0] >= 0 &&
593 +                                                        pos[0] < 1 &&
594 +                                                        pos[1] >= 0 &&
595 +                                                        pos[1] < 1) {
596 +                                                xl[y].min = x - step + 1;
597 +                                                break;
598 +                                        }
599 +                                }
600 +                                if (y < yl->min)                /* y limits */
601 +                                        yl->min = y - step + 1;
602 +                                yl->max = y + step - 1;
603 +                                break;
604 +                        }
605 +                }
606 +                                                        /* fill in between */
607 +                if (y < step) {
608 +                        xl[y-1].min = xl[y].min;
609 +                        xl[y-1].max = xl[y].max;
610 +                } else {
611 +                        if (xl[y].min < xl[y-step].min)
612 +                                xl[y-1].min = xl[y].min;
613 +                        else
614 +                                xl[y-1].min = xl[y-step].min;
615 +                        if (xl[y].max > xl[y-step].max)
616 +                                xl[y-1].max = xl[y].max;
617 +                        else
618 +                                xl[y-1].max = xl[y-step].max;
619 +                }
620 +                for (x = 2; x < step; x++)
621 +                        copystruct(xl+y-x, xl+y-1);
622 +        }
623 +        if (yl->max >= numscans(&tresolu))
624 +                yl->max = numscans(&tresolu) - 1;
625 +        for (x = numscans(&tresolu) - 1; x > y; x--)    /* fill bottom rows */
626 +                copystruct(xl+x, xl+y);
627 +        return(yl->max >= yl->min);
628 + }
629 +
630 +
631   backpicture(fill, samp)                 /* background fill algorithm */
632   int     (*fill)();
633   int     samp;
# Line 522 | Line 638 | int    samp;
638                                                          /* get back buffer */
639          yback = (int *)malloc(hresolu*sizeof(int));
640          if (yback == NULL)
641 <                syserror();
641 >                syserror(progname);
642          for (x = 0; x < hresolu; x++)
643                  yback[x] = -2;
644          /*
# Line 619 | Line 735 | int    (*fill)();
735   }
736  
737  
738 + clipaft()                       /* perform aft clipping as indicated */
739 + {
740 +        register int    x, y;
741 +        double  tstdist;
742 +        double  yzn2, vx;
743 +
744 +        if (ourview.vaft <= FTINY)
745 +                return;
746 +        tstdist = ourview.vaft - ourview.vfore;
747 +        for (y = 0; y < vresolu; y++) {
748 +                if (ourview.type == VT_PER) {           /* adjust distance */
749 +                        yzn2 = (y+.5)/vresolu + ourview.voff - .5;
750 +                        yzn2 = 1. + yzn2*yzn2*ourview.vn2;
751 +                        tstdist = (ourview.vaft - ourview.vfore)*sqrt(yzn2);
752 +                }
753 +                for (x = 0; x < hresolu; x++)
754 +                        if (zscan(y)[x] > tstdist) {
755 +                                if (ourview.type == VT_PER) {
756 +                                        vx = (x+.5)/hresolu + ourview.hoff - .5;
757 +                                        if (zscan(y)[x] <= (ourview.vaft -
758 +                                                        ourview.vfore) *
759 +                                                sqrt(vx*vx*ourview.hn2 + yzn2))
760 +                                                continue;
761 +                                }
762 +                                bzero(pscan(y)[x], sizeof(COLR));
763 +                                zscan(y)[x] = 0.0;
764 +                        }
765 +        }
766 + }
767 +
768 +
769   writepicture()                          /* write out picture */
770   {
771          int     y;
# Line 626 | Line 773 | writepicture()                         /* write out picture */
773          fprtresolu(hresolu, vresolu, stdout);
774          for (y = vresolu-1; y >= 0; y--)
775                  if (fwritecolrs(pscan(y), hresolu, stdout) < 0)
776 <                        syserror();
776 >                        syserror(progname);
777   }
778  
779  
# Line 638 | Line 785 | char   *fname;
785          int     y;
786          float   *zout;
787  
788 <        if ((fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1) {
789 <                perror(fname);
643 <                exit(1);
644 <        }
788 >        if ((fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1)
789 >                syserror(fname);
790          if (donorm
791          && (zout = (float *)malloc(hresolu*sizeof(float))) == NULL)
792 <                syserror();
792 >                syserror(progname);
793          for (y = vresolu-1; y >= 0; y--) {
794                  if (donorm) {
795                          double  vx, yzn2;
# Line 659 | Line 804 | char   *fname;
804                  } else
805                          zout = zscan(y);
806                  if (write(fd, (char *)zout, hresolu*sizeof(float))
807 <                                < hresolu*sizeof(float)) {
808 <                        perror(fname);
664 <                        exit(1);
665 <                }
807 >                                < hresolu*sizeof(float))
808 >                        syserror(fname);
809          }
810          if (donorm)
811                  free((char *)zout);
# Line 708 | Line 851 | char   *prog, *args;
851          cp = combuf;
852          wp = argv;
853          for ( ; ; ) {
854 <                while (isspace(*cp)) cp++;
855 <                if (!*cp) break;
856 <                *wp++ = cp;
857 <                while (!isspace(*cp))
858 <                        if (!*cp++) goto done;
859 <                *cp++ = '\0';
854 >                while (isspace(*cp))    /* nullify spaces */
855 >                        *cp++ = '\0';
856 >                if (!*cp)               /* all done? */
857 >                        break;
858 >                *wp++ = cp;             /* add argument to list */
859 >                while (*++cp && !isspace(*cp))
860 >                        ;
861          }
718 done:
862          *wp = NULL;
863                                                  /* start process */
864          if ((rval = open_process(PDesc, argv)) < 0)
865 <                syserror();
865 >                syserror(progname);
866          if (rval == 0) {
867                  fprintf(stderr, "%s: command not found\n", argv[0]);
868                  exit(1);
# Line 760 | Line 903 | clearqueue()                           /* process queue */
903          register float  *fbp;
904          register int    i;
905  
906 +        if (queuesiz == 0)
907 +                return;
908          fbp = fbuf;
909          for (i = 0; i < queuesiz; i++) {
910                  viewray(orig, dir, &ourview,
# Line 793 | Line 938 | clearqueue()                           /* process queue */
938   }
939  
940  
941 < syserror()                      /* report error and exit */
941 > syserror(s)                     /* report error and exit */
942 > char    *s;
943   {
944 <        perror(progname);
944 >        perror(s);
945          exit(1);
946   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines