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.23 by greg, Fri Jan 6 13:19:37 1995 UTC vs.
Revision 2.32 by gwlarson, Fri Jul 16 16:11:31 1999 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1994 Regents of the University of California */
1 > /* Copyright (c) 1999 Silicon Graphics, Inc. */
2  
3   #ifndef lint
4 < static char SCCSid[] = "$SunId$ LBL";
4 > static char SCCSid[] = "$SunId$ SGI";
5   #endif
6  
7   /*
# Line 26 | Line 26 | static char SCCSid[] = "$SunId$ LBL";
26   #define sscan(y)        (ourspict+(y)*hresolu)
27   #define wscan(y)        (ourweigh+(y)*hresolu)
28   #define zscan(y)        (ourzbuf+(y)*hresolu)
29 + #define bscan(y)        (ourbpict+(y)*hresolu)
30   #define averaging       (ourweigh != NULL)
31 + #define blurring        (ourbpict != NULL)
32   #define usematrix       (hasmatrix & !averaging)
33   #define zisnorm         (!usematrix | ourview.type != VT_PER)
34  
# Line 37 | Line 39 | static char SCCSid[] = "$SunId$ LBL";
39  
40   #define PACKSIZ         256             /* max. calculation packet size */
41  
42 < #define RTCOM           "rtrace -h- -ovl -fff "
42 > #define RTCOM           "rtrace -h- -ovl -fff -ld- -i- -I- "
43  
44   #define ABS(x)          ((x)>0?(x):-(x))
45  
# Line 59 | Line 61 | COLR   *ourpict;                       /* output picture (COLR's) */
61   COLOR   *ourspict;                      /* output pixel sums (averaging) */
62   float   *ourweigh = NULL;               /* output pixel weights (averaging) */
63   float   *ourzbuf;                       /* corresponding z-buffer */
64 + COLOR   *ourbpict = NULL;               /* blurred picture (view averaging) */
65  
66 + VIEW    avgview;                        /* average view for -B option */
67 + int     nvavg;                          /* number of views averaged */
68 +
69   char    *progname;
70  
71   int     fillo = F_FORE|F_BACK;          /* selected fill options */
# Line 70 | Line 76 | COLR   backcolr = BLKCOLR;             /* background color */
76   COLOR   backcolor = BLKCOLOR;           /* background color (float) */
77   double  backz = 0.0;                    /* background z value */
78   int     normdist = 1;                   /* i/o normalized distance? */
79 + char    ourfmt[LPICFMT+1] = PICFMT;     /* original picture format */
80   double  ourexp = -1;                    /* original picture exposure */
81   int     expadj = 0;                     /* exposure adjustment (f-stops) */
82   double  rexpadj = 1;                    /* real exposure adjustment */
83  
84 < VIEW    theirview = STDVIEW;            /* input view */
84 > VIEW    theirview;                      /* input view */
85   int     gotview;                        /* got input view? */
86   int     wrongformat = 0;                /* input in another format? */
87   RESOLU  tresolu;                        /* input resolution */
# Line 86 | Line 93 | int    PDesc[3] = {-1,-1,-1};          /* rtrace process descript
93   #define childpid        (PDesc[2])
94   unsigned short  queue[PACKSIZ][2];      /* pending pixels */
95   int     packsiz;                        /* actual packet size */
96 < int     queuesiz;                       /* number of pixels pending */
96 > int     queuesiz = 0;                   /* number of pixels pending */
97  
98   extern double   movepixel();
99  
# Line 95 | Line 102 | main(argc, argv)                       /* interpolate pictures */
102   int     argc;
103   char    *argv[];
104   {
105 < #define  check(ol,al)           if (argv[i][ol] || \
106 <                                badarg(argc-i-1,argv+i+1,al)) \
105 > #define  check(ol,al)           if (argv[an][ol] || \
106 >                                badarg(argc-an-1,argv+an+1,al)) \
107                                  goto badopt
108          int     gotvfile = 0;
109          int     doavg = -1;
110 +        int     doblur = 0;
111          char    *zfile = NULL;
112          char    *expcomp = NULL;
113 <        char    *err;
106 <        int     i, rval;
113 >        int     i, an, rval;
114  
115          progname = argv[0];
116  
117 <        for (i = 1; i < argc && argv[i][0] == '-'; i++) {
118 <                rval = getviewopt(&ourview, argc-i, argv+i);
117 >        for (an = 1; an < argc && argv[an][0] == '-'; an++) {
118 >                rval = getviewopt(&ourview, argc-an, argv+an);
119                  if (rval >= 0) {
120 <                        i += rval;
120 >                        an += rval;
121                          continue;
122                  }
123 <                switch (argv[i][1]) {
123 >                switch (argv[an][1]) {
124                  case 'e':                               /* exposure */
125                          check(2,"f");
126 <                        expcomp = argv[++i];
126 >                        expcomp = argv[++an];
127                          break;
128                  case 't':                               /* threshold */
129                          check(2,"f");
130 <                        zeps = atof(argv[++i]);
130 >                        zeps = atof(argv[++an]);
131                          break;
132                  case 'a':                               /* average */
133                          check(2,NULL);
134                          doavg = 1;
135                          break;
136 +                case 'B':                               /* blur views */
137 +                        check(2,NULL);
138 +                        doblur = 1;
139 +                        break;
140                  case 'q':                               /* quick (no avg.) */
141                          check(2,NULL);
142                          doavg = 0;
# Line 135 | Line 146 | char   *argv[];
146                          normdist = !normdist;
147                          break;
148                  case 'f':                               /* fill type */
149 <                        switch (argv[i][2]) {
149 >                        switch (argv[an][2]) {
150                          case '0':                               /* none */
151                                  check(3,NULL);
152                                  fillo = 0;
# Line 154 | Line 165 | char   *argv[];
165                                  break;
166                          case 's':                               /* sample */
167                                  check(3,"i");
168 <                                fillsamp = atoi(argv[++i]);
168 >                                fillsamp = atoi(argv[++an]);
169                                  break;
170                          case 'c':                               /* color */
171                                  check(3,"fff");
172                                  fillfunc = backfill;
173 <                                setcolor(backcolor, atof(argv[i+1]),
174 <                                        atof(argv[i+2]), atof(argv[i+3]));
173 >                                setcolor(backcolor, atof(argv[an+1]),
174 >                                        atof(argv[an+2]), atof(argv[an+3]));
175                                  setcolr(backcolr, colval(backcolor,RED),
176                                                  colval(backcolor,GRN),
177                                                  colval(backcolor,BLU));
178 <                                i += 3;
178 >                                an += 3;
179                                  break;
180                          case 'z':                               /* z value */
181                                  check(3,"f");
182                                  fillfunc = backfill;
183 <                                backz = atof(argv[++i]);
183 >                                backz = atof(argv[++an]);
184                                  break;
185                          case 'r':                               /* rtrace */
186                                  check(3,"s");
187                                  fillfunc = rcalfill;
188 <                                calstart(RTCOM, argv[++i]);
188 >                                calstart(RTCOM, argv[++an]);
189                                  break;
190                          default:
191                                  goto badopt;
# Line 182 | Line 193 | char   *argv[];
193                          break;
194                  case 'z':                               /* z file */
195                          check(2,"s");
196 <                        zfile = argv[++i];
196 >                        zfile = argv[++an];
197                          break;
198                  case 'x':                               /* x resolution */
199                          check(2,"i");
200 <                        hresolu = atoi(argv[++i]);
200 >                        hresolu = atoi(argv[++an]);
201                          break;
202                  case 'y':                               /* y resolution */
203                          check(2,"i");
204 <                        vresolu = atoi(argv[++i]);
204 >                        vresolu = atoi(argv[++an]);
205                          break;
206                  case 'p':                               /* pixel aspect */
207 <                        if (argv[i][2] != 'a')
207 >                        if (argv[an][2] != 'a')
208                                  goto badopt;
209                          check(3,"f");
210 <                        pixaspect = atof(argv[++i]);
210 >                        pixaspect = atof(argv[++an]);
211                          break;
212                  case 'v':                               /* view file */
213 <                        if (argv[i][2] != 'f')
213 >                        if (argv[an][2] != 'f')
214                                  goto badopt;
215                          check(3,"s");
216 <                        gotvfile = viewfile(argv[++i], &ourview, 0, 0);
216 >                        gotvfile = viewfile(argv[++an], &ourview, 0, 0);
217                          if (gotvfile < 0)
218 <                                syserror(argv[i]);
218 >                                syserror(argv[an]);
219                          else if (gotvfile == 0) {
220                                  fprintf(stderr, "%s: bad view file\n",
221 <                                                argv[i]);
221 >                                                argv[an]);
222                                  exit(1);
223                          }
224                          break;
225                  default:
226                  badopt:
227                          fprintf(stderr, "%s: command line error at '%s'\n",
228 <                                        progname, argv[i]);
228 >                                        progname, argv[an]);
229                          goto userr;
230                  }
231          }
232                                                  /* check arguments */
233 <        if ((argc-i)%2)
233 >        if ((argc-an)%2)
234                  goto userr;
235          if (fillsamp == 1)
236                  fillo &= ~F_BACK;
237          if (doavg < 0)
238 <                doavg = (argc-i) > 2;
238 >                doavg = (argc-an) > 2;
239          if (expcomp != NULL)
240                  if (expcomp[0] == '+' | expcomp[0] == '-') {
241                          expadj = atof(expcomp) + (expcomp[0]=='+' ? .5 : -.5);
242 <                        if (doavg)
242 >                        if (doavg | doblur)
243                                  rexpadj = pow(2.0, atof(expcomp));
244                          else
245                                  rexpadj = pow(2.0, (double)expadj);
# Line 237 | Line 248 | char   *argv[];
248                                  goto userr;
249                          rexpadj = atof(expcomp);
250                          expadj = log(rexpadj)/LOG2 + (rexpadj>1 ? .5 : -.5);
251 <                        if (!doavg)
251 >                        if (!(doavg | doblur))
252                                  rexpadj = pow(2.0, (double)expadj);
253                  }
254                                                  /* set view */
255 <        if ((err = setview(&ourview)) != NULL) {
256 <                fprintf(stderr, "%s: %s\n", progname, err);
255 >        if (nextview(doblur ? stdin : (FILE *)NULL) == EOF) {
256 >                fprintf(stderr, "%s: no view on standard input!\n",
257 >                                progname);
258                  exit(1);
259          }
260          normaspect(viewaspect(&ourview), &pixaspect, &hresolu, &vresolu);
# Line 257 | Line 269 | char   *argv[];
269                  if (ourpict == NULL)
270                          syserror(progname);
271          }
272 +        if (doblur) {
273 +                ourbpict = (COLOR *)bmalloc(hresolu*vresolu*sizeof(COLOR));
274 +                if (ourbpict == NULL)
275 +                        syserror(progname);
276 +        }
277          ourzbuf = (float *)bmalloc(hresolu*vresolu*sizeof(float));
278          if (ourzbuf == NULL)
279                  syserror(progname);
263        bzero((char *)ourzbuf, hresolu*vresolu*sizeof(float));
280                                                          /* new header */
281          newheader("RADIANCE", stdout);
282 <                                                        /* get input */
283 <        for ( ; i < argc; i += 2)
284 <                addpicture(argv[i], argv[i+1]);
285 <                                                        /* fill in spaces */
286 <        if (fillo&F_BACK)
287 <                backpicture(fillfunc, fillsamp);
288 <        else
289 <                fillpicture(fillfunc);
282 >                                                        /* run pictures */
283 >        do {
284 >                bzero((char *)ourzbuf, hresolu*vresolu*sizeof(float));
285 >                for (i = an; i < argc; i += 2)
286 >                        addpicture(argv[i], argv[i+1]);
287 >                if (fillo&F_BACK)                       /* fill in spaces */
288 >                        backpicture(fillfunc, fillsamp);
289 >                else
290 >                        fillpicture(fillfunc);
291 >                                                        /* aft clipping */
292 >                clipaft();
293 >        } while (addblur() && nextview(stdin) != EOF);
294                                                          /* close calculation */
295          caldone();
276                                                        /* aft clipping */
277        clipaft();
296                                                          /* add to header */
297          printargs(argc, argv, stdout);
298 <        if (gotvfile) {
298 >        compavgview();
299 >        if (doblur | gotvfile) {
300                  fputs(VIEWSTR, stdout);
301 <                fprintview(&ourview, stdout);
301 >                fprintview(&avgview, stdout);
302                  putc('\n', stdout);
303          }
304          if (pixaspect < .99 | pixaspect > 1.01)
# Line 290 | Line 309 | char   *argv[];
309                  ourexp = rexpadj;
310          if (ourexp < .995 | ourexp > 1.005)
311                  fputexpos(ourexp, stdout);
312 <        fputformat(COLRFMT, stdout);
312 >        if (strcmp(ourfmt, PICFMT))             /* print format if known */
313 >                fputformat(ourfmt, stdout);
314          putc('\n', stdout);
315                                                          /* write picture */
316          writepicture();
# Line 301 | Line 321 | char   *argv[];
321          exit(0);
322   userr:
323          fprintf(stderr,
324 <        "Usage: %s [view opts][-t eps][-z zout][-e spec][-a|-q][-fT][-n] pfile zspec ..\n",
324 >        "Usage: %s [view opts][-t eps][-z zout][-e spec][-B][-a|-q][-fT][-n] pfile zspec ..\n",
325                          progname);
326          exit(1);
327   #undef check
328   }
329  
330  
331 + int
332   headline(s)                             /* process header string */
333   char    *s;
334   {
335          char    fmt[32];
336  
337          if (isheadid(s))
338 <                return;
338 >                return(0);
339          if (formatval(fmt, s)) {
340 <                wrongformat = strcmp(fmt, COLRFMT);
341 <                return;
340 >                if (globmatch(ourfmt, fmt)) {
341 >                        wrongformat = 0;
342 >                        strcpy(ourfmt, fmt);
343 >                } else
344 >                        wrongformat = 1;
345 >                return(0);
346          }
347 <        putc('\t', stdout);
348 <        fputs(s, stdout);
349 <
347 >        if (nvavg < 2) {
348 >                putc('\t', stdout);
349 >                fputs(s, stdout);
350 >        }
351          if (isexpos(s)) {
352                  theirexp *= exposval(s);
353 <                return;
353 >                return(0);
354          }
355          if (isview(s) && sscanview(&theirview, s) > 0)
356                  gotview++;
357 +        return(0);
358   }
359  
360  
361 + nextview(fp)                            /* get and set next view */
362 + FILE    *fp;
363 + {
364 +        char    linebuf[256];
365 +        char    *err;
366 +        register int    i;
367 +
368 +        if (fp != NULL) {
369 +                do                      /* get new view */
370 +                        if (fgets(linebuf, sizeof(linebuf), fp) == NULL)
371 +                                return(EOF);
372 +                while (!isview(linebuf) || !sscanview(&ourview, linebuf));
373 +        }
374 +                                        /* set new view */
375 +        if ((err = setview(&ourview)) != NULL) {
376 +                fprintf(stderr, "%s: %s\n", progname, err);
377 +                exit(1);
378 +        }
379 +        if (!nvavg) {                   /* first view */
380 +                copystruct(&avgview, &ourview);
381 +                return(nvavg++);
382 +        }
383 +                                        /* add to average view */
384 +        for (i = 0; i < 3; i++) {
385 +                avgview.vp[i] += ourview.vp[i];
386 +                avgview.vdir[i] += ourview.vdir[i];
387 +                avgview.vup[i] += ourview.vup[i];
388 +        }
389 +        avgview.horiz += ourview.horiz;
390 +        avgview.vert += ourview.vert;
391 +        avgview.hoff += ourview.hoff;
392 +        avgview.voff += ourview.voff;
393 +        avgview.vfore += ourview.vfore;
394 +        avgview.vaft += ourview.vaft;
395 +        return(nvavg++);
396 + }
397 +
398 +
399 + compavgview()                           /* compute average view */
400 + {
401 +        register int    i;
402 +        double  f;
403 +
404 +        if (nvavg < 2)
405 +                return;
406 +        f = 1.0/nvavg;
407 +        for (i = 0; i < 3; i++) {
408 +                avgview.vp[i] *= f;
409 +                avgview.vdir[i] *= f;
410 +                avgview.vup[i] *= f;
411 +        }
412 +        avgview.horiz *= f;
413 +        avgview.vert *= f;
414 +        avgview.hoff *= f;
415 +        avgview.voff *= f;
416 +        avgview.vfore *= f;
417 +        avgview.vaft *= f;
418 +        if (setview(&avgview) != NULL)          /* in case of emergency... */
419 +                copystruct(&avgview, &ourview);
420 +        pixaspect = viewaspect(&avgview) * hresolu / vresolu;
421 + }
422 +
423 +
424   addpicture(pfile, zspec)                /* add picture to output */
425   char    *pfile, *zspec;
426   {
# Line 347 | Line 437 | char   *pfile, *zspec;
437                  syserror(pfile);
438                                          /* get header with exposure and view */
439          theirexp = 1.0;
440 +        copystruct(&theirview, &stdview);
441          gotview = 0;
442 <        printf("%s:\n", pfile);
442 >        if (nvavg < 2)
443 >                printf("%s:\n", pfile);
444          getheader(pfp, headline, NULL);
445          if (wrongformat || !gotview || !fgetsresolu(&tresolu, pfp)) {
446                  fprintf(stderr, "%s: picture format error\n", pfile);
# Line 432 | Line 524 | register VIEW  *vw1, *vw2;
524   {
525          double  m4t[4][4];
526  
527 <        if (vw1->type != VT_PER && vw1->type != VT_PAR)
527 >        if (vw1->type != VT_PER & vw1->type != VT_PAR)
528                  return(0);
529 <        if (vw2->type != VT_PER && vw2->type != VT_PAR)
529 >        if (vw2->type != VT_PER & vw2->type != VT_PAR)
530                  return(0);
531          setident4(xfmat);
532          xfmat[0][0] = vw1->hvec[0];
# Line 551 | Line 643 | double z;
643                  y1 = p0->y + c1*s1y/l1;
644                  for (c2 = l2; c2-- > 0; ) {
645                          x = x1 + c2*s2x/l2;
646 <                        if (x < 0 || x >= hresolu)
646 >                        if (x < 0 | x >= hresolu)
647                                  continue;
648                          y = y1 + c2*s2y/l2;
649 <                        if (y < 0 || y >= vresolu)
649 >                        if (y < 0 | y >= vresolu)
650                                  continue;
651                          if (averaging) {
652                                  if (zscan(y)[x] <= 0 || zscan(y)[x]-z
# Line 582 | Line 674 | register FVECT pos;
674   {
675          FVECT   pt, tdir, odir;
676          double  d;
585        register int    i;
677          
678          if (pos[2] <= 0)                /* empty pixel */
679                  return(0);
680          if (usematrix) {
681                  pos[0] += theirview.hoff - .5;
682                  pos[1] += theirview.voff - .5;
683 <                if (normdist && theirview.type == VT_PER)
683 >                if (normdist & theirview.type == VT_PER)
684                          d = sqrt(1. + pos[0]*pos[0]*theirview.hn2
685                                          + pos[1]*pos[1]*theirview.vn2);
686                  else
# Line 623 | Line 714 | register FVECT pos;
714                  if (pos[2] <= 0)
715                          return(0);
716          }
717 <        if (pos[0] < 0 || pos[0] >= 1-FTINY || pos[1] < 0 || pos[1] >= 1-FTINY)
717 >        if (pos[0] < 0 | pos[0] >= 1-FTINY | pos[1] < 0 | pos[1] >= 1-FTINY)
718                  return(0);
719          if (!averaging)
720                  return(1);
721 <        if (ourview.type == VT_PAR)             /* compute our direction */
722 <                VCOPY(odir, ourview.vdir);
723 <        else
724 <                for (i = 0; i < 3; i++)
725 <                        odir[i] = (pt[i] - ourview.vp[i])/pos[2];
726 <        d = DOT(odir,tdir);                     /* compute pixel weight */
727 <        if (d >= 1.-1./MAXWT/MAXWT)
721 >                                                /* compute pixel weight */
722 >        if (ourview.type == VT_PAR) {
723 >                d = DOT(ourview.vdir,tdir);
724 >                d = 1. - d*d;
725 >        } else {
726 >                VSUB(odir, pt, ourview.vp);
727 >                d = DOT(odir,tdir);
728 >                d = 1. - d*d/DOT(odir,odir);
729 >        }
730 >        if (d <= 1./MAXWT/MAXWT)
731                  return(MAXWT);          /* clip to maximum weight */
732 <        return(1./sqrt(1.-d));
732 >        return(1./sqrt(d));
733   }
734  
735  
# Line 840 | Line 934 | int    (*fill)();
934                  for (x = 0; x < hresolu; x++)
935                          if (zscan(y)[x] <= 0)
936                                  (*fill)(x,y);
937 +        if (fill == rcalfill)
938 +                clearqueue();
939   }
940  
941  
# Line 851 | Line 947 | clipaft()                      /* perform aft clipping as indicated */
947          double  yzn2, vx;
948  
949          if (ourview.vaft <= FTINY)
950 <                return;
950 >                return(0);
951          tstdist = ourview.vaft - ourview.vfore;
952          for (y = 0; y < vresolu; y++) {
953                  if (adjtest) {                          /* adjust test */
# Line 875 | Line 971 | clipaft()                      /* perform aft clipping as indicated */
971                                  zscan(y)[x] = 0.0;
972                          }
973          }
974 +        return(1);
975   }
976  
977  
978 + addblur()                               /* add to blurred picture */
979 + {
980 +        COLOR   cval;
981 +        double  d;
982 +        register int    i;
983 +
984 +        if (!blurring)
985 +                return(0);
986 +        i = hresolu*vresolu;
987 +        if (nvavg < 2)
988 +                if (averaging)
989 +                        while (i--) {
990 +                                copycolor(ourbpict[i], ourspict[i]);
991 +                                d = 1.0/ourweigh[i];
992 +                                scalecolor(ourbpict[i], d);
993 +                        }
994 +                else
995 +                        while (i--)
996 +                                colr_color(ourbpict[i], ourpict[i]);
997 +        else
998 +                if (averaging)
999 +                        while (i--) {
1000 +                                copycolor(cval, ourspict[i]);
1001 +                                d = 1.0/ourweigh[i];
1002 +                                scalecolor(cval, d);
1003 +                                addcolor(ourbpict[i], cval);
1004 +                        }
1005 +                else
1006 +                        while (i--) {
1007 +                                colr_color(cval, ourpict[i]);
1008 +                                addcolor(ourbpict[i], cval);
1009 +                        }
1010 +                                /* print view */
1011 +        printf("VIEW%d:", nvavg);
1012 +        fprintview(&ourview, stdout);
1013 +        putchar('\n');
1014 +        return(1);
1015 + }
1016 +
1017 +
1018   writepicture()                          /* write out picture (alters buffer) */
1019   {
1020          int     y;
# Line 886 | Line 1023 | writepicture()                         /* write out picture (alters buffer)
1023  
1024          fprtresolu(hresolu, vresolu, stdout);
1025          for (y = vresolu-1; y >= 0; y--)
1026 <                if (averaging) {
1026 >                if (blurring) {
1027 >                        for (x = 0; x < hresolu; x++) { /* compute avg. */
1028 >                                d = rexpadj/nvavg;
1029 >                                scalecolor(bscan(y)[x], d);
1030 >                        }
1031 >                        if (fwritescan(bscan(y), hresolu, stdout) < 0)
1032 >                                syserror(progname);
1033 >                } else if (averaging) {
1034                          for (x = 0; x < hresolu; x++) { /* average pixels */
1035                                  d = rexpadj/wscan(y)[x];
1036                                  scalecolor(sscan(y)[x], d);
# Line 1029 | Line 1173 | clearqueue()                           /* process queue */
1173          }
1174                                          /* mark end and get results */
1175          bzero((char *)fbp, 6*sizeof(float));
1176 <        if (process(PDesc, fbuf, fbuf, 4*sizeof(float)*queuesiz,
1176 >        if (process(PDesc, fbuf, fbuf, 4*sizeof(float)*(queuesiz+1),
1177                          6*sizeof(float)*(queuesiz+1)) !=
1178 <                        4*sizeof(float)*queuesiz) {
1178 >                        4*sizeof(float)*(queuesiz+1)) {
1179                  fprintf(stderr, "%s: error reading from rtrace process\n",
1180                                  progname);
1181                  exit(1);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines