ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/ranimove1.c
(Generate patch)

Comparing ray/src/util/ranimove1.c (file contents):
Revision 3.12 by greg, Tue Dec 20 03:49:51 2005 UTC vs.
Revision 3.21 by greg, Fri Oct 5 00:59:38 2012 UTC

# Line 30 | Line 30 | float          *zbuffer;       /* depth at each pixel */
30   OBJECT          *obuffer;       /* object id at each pixel */
31   short           *xmbuffer;      /* x motion at each pixel */
32   short           *ymbuffer;      /* y motion at each pixel */
33 < BYTE            *abuffer;       /* accuracy at each pixel */
34 < BYTE            *sbuffer;       /* sample count per pixel */
33 > uby8            *abuffer;       /* accuracy at each pixel */
34 > uby8            *sbuffer;       /* sample count per pixel */
35 > COLOR           *outbuffer;     /* output buffer (may equal cbuffer) */
36  
37   VIEW            vwprev;         /* last frame's view */
38   COLOR           *cprev;         /* last frame colors */
39   float           *zprev;         /* last frame depth */
40   OBJECT          *oprev;         /* last frame objects */
41 < BYTE            *aprev;         /* last frame accuracy */
41 > uby8            *aprev;         /* last frame accuracy */
42  
43   float           *cerrmap;       /* conspicuous error map */
44   COLOR           *val2map;       /* value-squared map for variance */
# Line 48 | Line 49 | double         hlsmax;         /* maximum high-level saliency this f
49  
50  
51   static void next_frame(void);
52 < static int sample_here(int      x, int  y);
53 < static int offset_cmp(const void        *p1, const void *p2);
54 < static void setmotion(int       n, FVECT        wpos);
52 > static int sample_here(int x, int y);
53 > static int offset_cmp(const void *p1, const void *p2);
54 > static void setmotion(int n, FVECT wpos);
55   static void init_frame_sample(void);
56  
57  
58 < extern void
58 > #if 0
59 > void
60   write_map(              /* write out float map (debugging) */
61          float   *mp,
62          char    *fn
# Line 79 | Line 81 | write_map(             /* write out float map (debugging) */
81          }
82          fclose(fp);
83   }
84 + #endif
85  
86  
87   static void
# Line 105 | Line 108 | next_frame(void)                       /* prepare next frame buffer */
108                  error(USER, errmsg);
109          }
110          if (cbuffer == NULL) {
111 <                                        /* compute resolution and allocate */
111 >                int     n;              /* compute resolution and allocate */
112                  switch (sscanf(vval(RESOLUTION), "%d %d %lf",
113                                  &hres, &vres, &pixaspect)) {
114                  case 1:
# Line 128 | Line 131 | next_frame(void)                       /* prepare next frame buffer */
131                  obuffer = (OBJECT *)malloc(sizeof(OBJECT)*hres*vres);
132                  xmbuffer = (short *)malloc(sizeof(short)*hres*vres);
133                  ymbuffer = (short *)malloc(sizeof(short)*hres*vres);
134 <                abuffer = (BYTE *)calloc(hres*vres, sizeof(BYTE));
135 <                sbuffer = (BYTE *)calloc(hres*vres, sizeof(BYTE));
134 >                abuffer = (uby8 *)calloc(hres*vres, sizeof(uby8));
135 >                sbuffer = (uby8 *)calloc(hres*vres, sizeof(uby8));
136                  cprev = (COLOR *)malloc(sizeof(COLOR)*hres*vres);
137                  zprev = (float *)malloc(sizeof(float)*hres*vres);
138                  oprev = (OBJECT *)malloc(sizeof(OBJECT)*hres*vres);
139 <                aprev = (BYTE *)malloc(sizeof(BYTE)*hres*vres);
139 >                aprev = (uby8 *)malloc(sizeof(uby8)*hres*vres);
140 >                if (mblur > .02)
141 >                        outbuffer = (COLOR *)malloc(sizeof(COLOR)*hres*vres);
142 >                else
143 >                        outbuffer = cbuffer;
144                  if ((cbuffer==NULL) | (zbuffer==NULL) | (obuffer==NULL) |
145                                  (xmbuffer==NULL) | (ymbuffer==NULL) |
146                                  (abuffer==NULL) | (sbuffer==NULL) |
147                                  (cprev==NULL) | (zprev == NULL) |
148 <                                (oprev==NULL) | (aprev==NULL))
148 >                                (oprev==NULL) | (aprev==NULL) |
149 >                                (outbuffer==NULL))
150                          error(SYSTEM, "out of memory in init_frame");
151 +                for (n = hres*vres; n--; ) {
152 +                        zprev[n] = -1.f;
153 +                        oprev[n] = OVOID;
154 +                }
155                  frm_stop = getTime() + rtperfrm;
156 <        } else {
145 <                COLOR   *cp;            /* else just swap buffers */
156 >        } else {                        /* else just swap buffers */
157                  float   *fp;
158                  OBJECT  *op;
159 <                BYTE    *bp;
160 <                cp = cprev; cprev = cbuffer; cbuffer = cp;
159 >                uby8    *bp;
160 >                if (outbuffer != cbuffer) {
161 >                        COLOR   *cp = cprev;
162 >                        cprev = cbuffer; cbuffer = cp;
163 >                } else {
164 >                        outbuffer = cprev; cprev = cbuffer;
165 >                        cbuffer = outbuffer;
166 >                }
167                  fp = zprev; zprev = zbuffer; zbuffer = fp;
168                  op = oprev; oprev = obuffer; obuffer = op;
169                  bp = aprev; aprev = abuffer; abuffer = bp;
170 <                memset(abuffer, '\0', sizeof(BYTE)*hres*vres);
171 <                memset(sbuffer, '\0', sizeof(BYTE)*hres*vres);
170 >                memset(abuffer, 0, sizeof(uby8)*hres*vres);
171 >                memset(sbuffer, 0, sizeof(uby8)*hres*vres);
172                  frm_stop += rtperfrm;
173          }
174          cerrmap = NULL;
# Line 165 | Line 182 | next_frame(void)                       /* prepare next frame buffer */
182  
183   static int
184   sample_here(            /* 4x4 quincunx sample at this pixel? */
185 <        register int    x,
186 <        register int    y
185 >        int     x,
186 >        int     y
187   )
188   {
189          if (y & 0x1)            /* every other row has samples */
# Line 177 | Line 194 | sample_here(           /* 4x4 quincunx sample at this pixel? */
194   }
195  
196  
197 < extern void
197 > void
198   sample_pos(     /* compute jittered sample position */
199          double  hv[2],
200          int     x,
# Line 194 | Line 211 | sample_pos(    /* compute jittered sample position */
211   }
212  
213  
214 < extern double
214 > double
215   sample_wt(              /* compute interpolant sample weight */
216          int     xo,
217          int yo
# Line 228 | Line 245 | offset_cmp(            /* compare offset distances */
245   }
246  
247  
248 < extern int
248 > int
249   getclosest(     /* get nc closest neighbors on same object */
250          int     *iarr,
251          int     nc,
# Line 241 | Line 258 | getclosest(    /* get nc closest neighbors on same object
258          static int      ioffs[NSCHECK];
259          OBJECT  myobj;
260          int     i0, nf;
261 <        register int    i, j;
261 >        int     i, j;
262                                          /* get our object number */
263          myobj = obuffer[fndx(x, y)];
264                                          /* special case for borders */
# Line 305 | Line 322 | getclosest(    /* get nc closest neighbors on same object
322  
323   static void
324   setmotion(              /* compute motion vector for this pixel */
325 <                register int    n,
325 >                int     n,
326                  FVECT   wpos
327   )
328   {
# Line 344 | Line 361 | init_frame_sample(void)                /* sample our initial frame *
361   {
362          RAY     ir;
363          int     x, y;
364 <        register int    n;
364 >        int     n;
365  
366          if (!silent) {
367                  printf("\tComputing initial samples...");
# Line 355 | Line 372 | init_frame_sample(void)                /* sample our initial frame *
372              for (x = hres; x--; ) {
373                  double  hv[2];
374                  n = fndx(x, y);
375 <                xmbuffer[n] = MO_UNK;
359 <                ymbuffer[n] = MO_UNK;
375 >                xmbuffer[n] = ymbuffer[n] = MO_UNK;
376                  sample_pos(hv, x, y, 0);
377                  ir.rmax = viewray(ir.rorg, ir.rdir, &vw, hv[0], hv[1]);
378                  if (ir.rmax < -FTINY) {
# Line 369 | Line 385 | init_frame_sample(void)                /* sample our initial frame *
385                  if (!sample_here(x, y)) {       /* just cast */
386                          rayorigin(&ir, PRIMARY, NULL, NULL);
387                          if (!localhit(&ir, &thescene)) {
388 <                                if (ir.ro != &Aftplane)
389 <                                        sourcehit(&ir);
388 >                                if (ir.ro != &Aftplane && sourcehit(&ir)) {
389 >                                        rayshade(&ir, ir.ro->omod);
390 >                                        rayparticipate(&ir);
391 >                                }
392                                  copycolor(cbuffer[n], ir.rcol);
393                                  zbuffer[n] = ir.rot;
394                                  obuffer[n] = ir.robj;
# Line 399 | Line 417 | init_frame_sample(void)                /* sample our initial frame *
417                  zbuffer[n] = ir.rot;
418                  obuffer[n] = ir.robj;
419                  sbuffer[n] = 1;
420 <                if (ir.rot >= FHUGE)
420 >                if (ir.rot >= 0.99*FHUGE)
421                          abuffer[n] = ADISTANT;
422                  else {
423                          abuffer[n] = ALOWQ;
# Line 442 | Line 460 | init_frame_sample(void)                /* sample our initial frame *
460   }
461  
462  
463 < extern int
463 > int
464   getambcolor(            /* get ambient color for object if we can */
465                  COLOR   clr,
466                  int     obj
467   )
468   {
469 <        register OBJREC *op;
469 >        OBJREC  *op;
470  
471          if (obj == OVOID)
472                  return(0);
473 <        op = objptr(obj);
474 <        if ((op->otype == OBJ_INSTANCE) & (op->omod == OVOID))
473 >        op = objptr(obj);               /* search for material */
474 >        if (op->omod == OVOID)
475                  return(0);
476 <                                        /* search for material */
477 <        do {
478 <                if (op->omod == OVOID || ofun[op->otype].flags & T_X)
461 <                        return(0);
462 <                op = objptr(op->omod);
463 <        } while (!ismaterial(op->otype));
476 >        op = findmaterial(objptr(op->omod));
477 >        if (op == NULL)
478 >                return(0);
479          /*
480           * Since this routine is called to compute the difference
481           * from rendering with and without interreflections,
# Line 474 | Line 489 | getambcolor(           /* get ambient color for object if we ca
489                          if (lv[0] == op->oname[0] &&
490                                          !strcmp(lv+1, op->oname+1))
491                                  break;
492 <                if ((lv != NULL) != hirendparams.ambincl)
492 >                if ((lv != NULL) ^ hirendparams.ambincl)
493                          return(0);
494          }
495          switch (op->otype) {
# Line 513 | Line 528 | getambcolor(           /* get ambient color for object if we ca
528   }
529  
530  
531 < extern double
531 > double
532   estimaterr(             /* estimate relative error from samples */
533                  COLOR   cs,
534                  COLOR   cs2,
# Line 539 | Line 554 | estimaterr(            /* estimate relative error from samples *
554   }
555  
556  
557 < extern double
557 > double
558   comperr(                /* estimate relative error in neighborhood */
559                  int     *neigh,
560                  int     nc,
# Line 550 | Line 565 | comperr(               /* estimate relative error in neighborhood *
565          COLOR   ctmp;
566          int     i;
567          int     ns;
568 <        register int    n;
568 >        int     n;
569                                          /* add together samples */
570          setcolor(csum, 0., 0., 0.);
571          setcolor(csum2, 0., 0., 0.);
# Line 564 | Line 579 | comperr(               /* estimate relative error in neighborhood *
579                  }
580                  if (sbuffer[n] != 1)
581                          error(CONSISTENCY, "bad count in comperr");
582 <                setcolor(ctmp,
583 <                        colval(cbuffer[n],RED)*colval(cbuffer[n],RED),
569 <                        colval(cbuffer[n],GRN)*colval(cbuffer[n],GRN),
570 <                        colval(cbuffer[n],BLU)*colval(cbuffer[n],BLU));
582 >                copycolor(ctmp, cbuffer[n]);
583 >                multcolor(ctmp, ctmp);
584                  addcolor(csum2, ctmp);
585                  ns++;
586          }
# Line 575 | Line 588 | comperr(               /* estimate relative error in neighborhood *
588   }
589  
590  
591 < extern void
591 > void
592   comp_frame_error(void)          /* initialize frame error values */
593   {
594 <        BYTE    *edone = NULL;
594 >        uby8    *edone = NULL;
595          COLOR   objamb;
596          double  eest;
597          int     neigh[NSAMPOK];
598          int     nc;
599          int     x, y, i;
600 <        register int    n;
600 >        int     n;
601  
602          if (!silent) {
603                  printf("\tComputing error map\n");
# Line 602 | Line 615 | comp_frame_error(void)         /* initialize frame error valu
615                   * error should be less than the ambient value divided
616                   * by the returned ray value -- we take half of this.
617                   */
618 <                edone = (BYTE *)calloc(hres*vres, sizeof(BYTE));
619 <                for (y = vres; y--; )
607 <                    for (x = hres; x--; ) {
608 <                        n = fndx(x, y);
618 >                edone = (uby8 *)calloc(hres*vres, sizeof(uby8));
619 >                for (n = hres*vres; n--; ) {
620                          if ((abuffer[n] != ALOWQ) | (obuffer[n] == OVOID))
621                                  continue;
622                          if (!getambcolor(objamb, obuffer[n]))
# Line 622 | Line 633 | comp_frame_error(void)         /* initialize frame error valu
633                          else if (i >= ADISTANT/2) i = ADISTANT/2-1;
634                          abuffer[n] = i;
635                          edone[n] = 1;
636 <                    }
636 >                }
637          }
638                                          /* final statistical estimate */
639          for (y = vres; y--; )
# Line 654 | Line 665 | comp_frame_error(void)         /* initialize frame error valu
665   }
666  
667  
668 < extern void
668 > void
669   init_frame(void)                        /* render base (low quality) frame */
670   {
671          int     restart;
661
672                                          /* allocate/swap buffers */
673          next_frame();
674                                          /* check rendering status */
675          restart = (!nobjects || vdef(MOVE));
676          if (!restart && curparams != &lorendparams && nprocs > 1)
677                  restart = -1;
668        if (restart > 0) {
669                if (nprocs > 1)
670                        ray_pdone(1);
671                else
672                        ray_done(1);
673        }
678                                          /* post low quality parameters */
679          if (curparams != &lorendparams)
680                  ray_restore(curparams = &lorendparams);
# Line 702 | Line 706 | init_frame(void)                       /* render base (low quality) frame
706          init_frame_sample();
707                                          /* initialize frame error */
708          comp_frame_error();
709 < return;
709 > #if 0
710   {
711          float   *ebuf = (float *)malloc(sizeof(float)*hres*vres);
712          char    fnm[256];
713 <        register int    n;
713 >        int     n;
714          for (n = hres*vres; n--; )
715                  ebuf[n] = acctab[abuffer[n]];
716          sprintf(fnm, vval(BASENAME), fcur);
# Line 714 | Line 718 | return;
718          write_map(ebuf, fnm);
719          free((void *)ebuf);
720   }
721 + #endif
722   }
723  
724  
725 < extern void
725 > void
726   filter_frame(void)                      /* interpolation, motion-blur, and exposure */
727   {
728 <        double  expval = expspec_val(getexp(fcur));
729 <        int     x, y;
730 <        int     neigh[NPINTERP];
731 <        int     nc;
732 <        COLOR   cval;
733 <        double  w, wsum;
734 <        register int    n;
728 >        const double    expval = expspec_val(getexp(fcur));
729 >        int             x, y;
730 >        int             neigh[NPINTERP];
731 >        int             nc;
732 >        COLOR           cval;
733 >        double          w, wsum;
734 >        int             n;
735  
736   #if 0
737          /* XXX TEMPORARY!! */
# Line 740 | Line 745 | filter_frame(void)                     /* interpolation, motion-blur, an
745                  free((void *)ebuf);
746          }
747   #endif
743
748          if (!silent) {
749                  printf("\tFiltering frame\n");
750                  fflush(stdout);
751          }
752                                          /* normalize samples */
753 <        for (y = vres; y--; )
750 <            for (x = hres; x--; ) {
751 <                n = fndx(x, y);
753 >        for (n = hres*vres; n--; ) {
754                  if (sbuffer[n] <= 1)
755                          continue;
756                  w = 1.0/(double)sbuffer[n];
757                  scalecolor(cbuffer[n], w);
758 <            }
758 >        }
759                                          /* interpolate samples */
760          for (y = vres; y--; )
761              for (x = hres; x--; ) {
# Line 785 | Line 787 | filter_frame(void)                     /* interpolation, motion-blur, an
787                  scalecolor(cbuffer[n], w);
788              }
789                                          /* motion blur if requested */
790 <        if (mblur > .02) {
790 >        if (outbuffer != cbuffer) {
791                  int     xs, ys, xl, yl;
792                  int     rise, run;
793                  long    rise2, run2;
794                  int     n2;
795                  int     cnt;
796 + fprintf(stderr, "outbuffer=0x%lx, cbuffer=0x%lx, cprev=0x%lx\n",
797 + (size_t)outbuffer, (size_t)cbuffer, (size_t)cprev);
798                                          /* sum in motion streaks */
799 <                memset(outbuffer, '\0', sizeof(COLOR)*hres*vres);
800 <                memset(wbuffer, '\0', sizeof(float)*hres*vres);
799 >                memset(outbuffer, 0, sizeof(COLOR)*hres*vres);
800 >                memset(wbuffer, 0, sizeof(float)*hres*vres);
801                  for (y = vres; y--; )
802                      for (x = hres; x--; ) {
803                          n = fndx(x, y);
# Line 808 | Line 812 | filter_frame(void)                     /* interpolation, motion-blur, an
812                                  wbuffer[n] += 1.;
813                                  continue;
814                          }
815 <                        xl = x - run/4;
812 <                        yl = y - rise/4;
815 >                        xl = x; yl = y;
816                          if (run < 0) { xs = -1; run = -run; }
817                          else xs = 1;
818                          if (rise < 0) { ys = -1; rise = -rise; }
# Line 860 | Line 863 | filter_frame(void)                     /* interpolation, motion-blur, an
863                          }
864                      }
865                                          /* compute final results */
863                for (y = vres; y--; )
864                    for (x = hres; x--; ) {
865                        n = fndx(x, y);
866                        if (wbuffer[n] <= FTINY)
867                                continue;
868                        w = 1./wbuffer[n];
869                        scalecolor(outbuffer[n], w);
870                    }
871        } else
866                  for (n = hres*vres; n--; )
867 <                        copycolor(outbuffer[n], cbuffer[n]);
867 >                        if (wbuffer[n] > 1.02) {
868 >                                w = 1./wbuffer[n];
869 >                                scalecolor(outbuffer[n], w);
870 >                        } else if (wbuffer[n] < 0.98) {
871 >                                w = 1.-wbuffer[n];
872 >                                copycolor(cval, cprev[n]);
873 >                                scalecolor(cval, w);
874 >                                addcolor(outbuffer[n], cval);
875 >                        }
876 >        }
877 >                                        /* adjust exposure */
878 >        for (n = ((expval < 0.98) | (expval > 1.02))*hres*vres; n--; )
879 >                scalecolor(outbuffer[n], expval);
880          /*
881             for (n = hres*vres; n--; )
882                     if (!sbuffer[n])
883                             setcolor(outbuffer[n], 0., 0., 0.);
884           */
879        /* adjust exposure */
880        if ((expval < 0.99) | (expval > 1.01))
881                for (n = hres*vres; n--; )
882                        scalecolor(outbuffer[n], expval);
885   #if 0
886          {
887                  float   *sbuf = (float *)malloc(sizeof(float)*hres*vres);
# Line 895 | Line 897 | filter_frame(void)                     /* interpolation, motion-blur, an
897   }
898  
899  
900 < extern void
900 > void
901   send_frame(void)                        /* send frame to destination */
902   {
903 <        char    pfname[1024];
903 >        char    fname[1024];
904          double  d;
905          FILE    *fp;
906          int     y;
907                                          /* open output picture */
908 <        sprintf(pfname, vval(BASENAME), fcur);
909 <        strcat(pfname, ".pic");
910 <        fp = fopen(pfname, "w");
908 >        sprintf(fname, vval(BASENAME), fcur);
909 >        strcat(fname, ".hdr");
910 >        fp = fopen(fname, "w");
911          if (fp == NULL) {
912 <                sprintf(errmsg, "cannot open output frame \"%s\"", pfname);
912 >                sprintf(errmsg, "cannot open output frame \"%s\"", fname);
913                  error(SYSTEM, errmsg);
914          }
915          SET_FILE_BINARY(fp);
916          if (!silent) {
917 <                printf("\tWriting to \"%s\"\n", pfname);
917 >                printf("\tWriting to \"%s\"\n", fname);
918                  fflush(stdout);
919          }
920                                          /* write header */
# Line 941 | Line 943 | send_frame(void)                       /* send frame to destination */
943                          goto writerr;
944          if (fclose(fp) == EOF)
945                  goto writerr;
946 +        if (vdef(ZNAME)) {              /* output z-buffer */
947 +                sprintf(fname, vval(ZNAME), fcur);
948 +                strcat(fname, ".zbf");
949 +                fp = fopen(fname, "w");
950 +                if (fp == NULL) {
951 +                        sprintf(errmsg, "cannot open z-file \"%s\"\n", fname);
952 +                        error(SYSTEM, errmsg);
953 +                }
954 +                SET_FILE_BINARY(fp);
955 +                if (!silent) {
956 +                        printf("\tWriting depths to \"%s\"\n", fname);
957 +                        fflush(stdout);
958 +                }
959 +                for (y = vres; y--; )
960 +                        if (fwrite(zbuffer+y*hres, sizeof(float),
961 +                                                hres, fp) != hres)
962 +                                goto writerr;
963 +                if (fclose(fp) == EOF)
964 +                        goto writerr;
965 +        }
966 +        if (vdef(MNAME)) {              /* output motion buffer */
967 +                unsigned short  *mbuffer = (unsigned short *)malloc(
968 +                                                sizeof(unsigned short)*3*hres);
969 +                int             x, n;
970 +                if (mbuffer == NULL)
971 +                        error(SYSTEM, "out of memory in send_frame");
972 +                sprintf(fname, vval(MNAME), fcur);
973 +                strcat(fname, ".mvo");
974 +                fp = fopen(fname, "w");
975 +                if (fp == NULL) {
976 +                        sprintf(errmsg, "cannot open motion file \"%s\"\n",
977 +                                                        fname);
978 +                        error(SYSTEM, errmsg);
979 +                }
980 +                SET_FILE_BINARY(fp);
981 +                if (!silent) {
982 +                        printf("\tWriting motion vectors to \"%s\"\n", fname);
983 +                        fflush(stdout);
984 +                }
985 +                for (y = vres; y--; ) {
986 +                        for (x = hres; x--; ) {
987 +                                n = fndx(x,y);
988 +                                mbuffer[3*x] = xmbuffer[n] + 0x8000;
989 +                                mbuffer[3*x+1] = ymbuffer[n] + 0x8000;
990 +                                mbuffer[3*x+2] = (oprev[n]!=OVOID)*0x8000;
991 +                        }
992 +                        if (fwrite(mbuffer, sizeof(*mbuffer),
993 +                                                3*hres, fp) != 3*hres)
994 +                                goto writerr;
995 +                }
996 +                free((void *)mbuffer);
997 +                if (fclose(fp) == EOF)
998 +                        goto writerr;
999 +        }
1000          return;                         /* all is well */
1001   writerr:
1002 <        sprintf(errmsg, "error writing frame \"%s\"", pfname);
1002 >        sprintf(errmsg, "error writing file \"%s\"", fname);
1003          error(SYSTEM, errmsg);
1004   }
1005  
1006  
1007 < extern void
1007 > void
1008   free_frame(void)                        /* free frame allocation */
1009   {
1010          if (cbuffer == NULL)
1011                  return;
1012 +        if (outbuffer != cbuffer)
1013 +                free((void *)outbuffer);
1014 +        outbuffer = NULL;
1015          free((void *)cbuffer); cbuffer = NULL;
1016          free((void *)zbuffer); zbuffer = NULL;
1017          free((void *)obuffer); obuffer = NULL;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines