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.2 by greg, Tue Feb 25 02:47:24 2003 UTC vs.
Revision 3.22 by greg, Fri Oct 5 01:03:22 2012 UTC

# Line 4 | Line 4 | static const char      RCSid[] = "$Id$";
4   /*
5   *  ranimove1.c
6   *
7 < *  Basic frame rendering routines for ranimate(1).
7 > *  Basic frame rendering routines for ranimove(1).
8   *
9   *  Created by Gregory Ward on Wed Jan 08 2003.
10   */
11  
12   #include "copyright.h"
13  
14 + #include <string.h>
15 +
16 + #include "platform.h"
17   #include "ranimove.h"
18   #include "otypes.h"
19 + #include "source.h"
20   #include "random.h"
21  
22   double          acctab[256];    /* accuracy value table */
# Line 26 | 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  
36   VIEW            vwprev;         /* last frame's view */
37   COLOR           *cprev;         /* last frame colors */
38   float           *zprev;         /* last frame depth */
39   OBJECT          *oprev;         /* last frame objects */
40 < BYTE            *aprev;         /* last frame accuracy */
40 > uby8            *aprev;         /* last frame accuracy */
41  
42   float           *cerrmap;       /* conspicuous error map */
43   COLOR           *val2map;       /* value-squared map for variance */
# Line 43 | Line 47 | double         frm_stop;       /* when to stop rendering this frame
47   double          hlsmax;         /* maximum high-level saliency this frame */
48  
49  
50 + static void next_frame(void);
51 + static int sample_here(int x, int y);
52 + static int offset_cmp(const void *p1, const void *p2);
53 + static void setmotion(int n, FVECT wpos);
54 + static void init_frame_sample(void);
55 +
56 +
57 + #if 0
58   void
59 < write_map(mp, fn)               /* write out float map (debugging) */
60 < float   *mp;
61 < char    *fn;
59 > write_map(              /* write out float map (debugging) */
60 >        float   *mp,
61 >        char    *fn
62 > )
63   {
64          FILE    *fp = fopen(fn, "w");
65          COLOR   scanbuf[2048];
# Line 67 | Line 80 | char   *fn;
80          }
81          fclose(fp);
82   }
83 + #endif
84  
85  
86   static void
87 < next_frame()                    /* prepare next frame buffer */
87 > next_frame(void)                        /* prepare next frame buffer */
88   {
89          VIEW    *fv;
90          char    *err;
91                                          /* get previous view */
92          if (vw.type != 0)
93 <                copystruct(&vwprev, &vw);
93 >                vwprev = vw;
94          else if (fcur > 1 && (fv = getview(fcur-1)) != NULL) {
95 <                copystruct(&vwprev, fv);
95 >                vwprev = *fv;
96                  if (setview(&vwprev) != NULL)
97                          vwprev.type = 0;
98          }
# Line 87 | Line 101 | next_frame()                   /* prepare next frame buffer */
101                  sprintf(errmsg, "cannot get view for frame %d", fcur);
102                  error(USER, errmsg);
103          }
104 <        copystruct(&vw, fv);
104 >        vw = *fv;
105          if ((err = setview(&vw)) != NULL) {
106                  sprintf(errmsg, "view error at frame %d: %s", fcur, err);
107                  error(USER, errmsg);
108          }
109          if (cbuffer == NULL) {
110 +                int     n;
111                                          /* compute resolution and allocate */
112                  switch (sscanf(vval(RESOLUTION), "%d %d %lf",
113                                  &hres, &vres, &pixaspect)) {
# Line 103 | Line 118 | next_frame()                   /* prepare next frame buffer */
118                          pixaspect = 1.;
119                          /* fall through */
120                  case 3:
121 <                        if ((hres > 0 & vres > 0))
121 >                        if ((hres > 0) & (vres > 0))
122                                  break;
123                          /* fall through */
124                  default:
# Line 116 | Line 131 | next_frame()                   /* 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);
140 <                if ((cbuffer==NULL | zbuffer==NULL | obuffer==NULL |
141 <                                xmbuffer==NULL | ymbuffer==NULL |
142 <                                abuffer==NULL | sbuffer==NULL |
143 <                                cprev==NULL | zprev == NULL |
144 <                                oprev==NULL | aprev==NULL))
139 >                aprev = (uby8 *)malloc(sizeof(uby8)*hres*vres);
140 >                if ((cbuffer==NULL) | (zbuffer==NULL) | (obuffer==NULL) |
141 >                                (xmbuffer==NULL) | (ymbuffer==NULL) |
142 >                                (abuffer==NULL) | (sbuffer==NULL) |
143 >                                (cprev==NULL) | (zprev == NULL) |
144 >                                (oprev==NULL) | (aprev==NULL))
145                          error(SYSTEM, "out of memory in init_frame");
146 +                for (n = hres*vres; n--; ) {
147 +                        zprev[n] = -1.f;
148 +                        oprev[n] = OVOID;
149 +                }
150                  frm_stop = getTime() + rtperfrm;
151          } else {
152                  COLOR   *cp;            /* else just swap buffers */
153                  float   *fp;
154                  OBJECT  *op;
155 <                BYTE    *bp;
155 >                uby8    *bp;
156                  cp = cprev; cprev = cbuffer; cbuffer = cp;
157                  fp = zprev; zprev = zbuffer; zbuffer = fp;
158                  op = oprev; oprev = obuffer; obuffer = op;
159                  bp = aprev; aprev = abuffer; abuffer = bp;
160 <                bzero(abuffer, sizeof(BYTE)*hres*vres);
161 <                bzero(sbuffer, sizeof(BYTE)*hres*vres);
160 >                memset(abuffer, '\0', sizeof(uby8)*hres*vres);
161 >                memset(sbuffer, '\0', sizeof(uby8)*hres*vres);
162                  frm_stop += rtperfrm;
163          }
164          cerrmap = NULL;
# Line 152 | Line 171 | next_frame()                   /* prepare next frame buffer */
171  
172  
173   static int
174 < sample_here(x, y)               /* 4x4 quincunx sample at this pixel? */
175 < register int    x, y;
174 > sample_here(            /* 4x4 quincunx sample at this pixel? */
175 >        int     x,
176 >        int     y
177 > )
178   {
179          if (y & 0x1)            /* every other row has samples */
180                  return(0);
# Line 164 | Line 185 | register int   x, y;
185  
186  
187   void
188 < sample_pos(hv, x, y, sn)        /* compute jittered sample position */
189 < double  hv[2];
190 < int     x, y;
191 < int     sn;
188 > sample_pos(     /* compute jittered sample position */
189 >        double  hv[2],
190 >        int     x,
191 >        int     y,
192 >        int     sn
193 > )
194   {
195          int     hl[2];
196  
# Line 179 | Line 202 | int    sn;
202  
203  
204   double
205 < sample_wt(xo, yo)               /* compute interpolant sample weight */
206 < int     xo, yo;
205 > sample_wt(              /* compute interpolant sample weight */
206 >        int     xo,
207 >        int yo
208 > )
209   {
210          static double   etab[400];
211 <        int     rad2 = xo*xo + yo*yo;
211 >        /* we can't use the name rad2 here, for some reason Visual C
212 >           thinks that is a constant (compiler bug?) */
213 >        int     rad_2 = xo*xo + yo*yo;
214          int     i;
215  
216          if (etab[0] <= FTINY)           /* initialize exponent table */
# Line 191 | Line 218 | int    xo, yo;
218                          etab[i] = exp(-0.1*i);
219  
220                                          /* look up Gaussian */
221 <        i = (int)((10.*3./(double)SAMPDIST2)*rad2 + .5);
221 >        i = (int)((10.*3./(double)SAMPDIST2)*rad_2 + .5);
222          if (i >= 400)
223                  return(0.0);
224          return(etab[i]);
# Line 199 | Line 226 | int    xo, yo;
226  
227  
228   static int
229 < offset_cmp(p1, p2)              /* compare offset distances */
230 < const void      *p1, *p2;
229 > offset_cmp(             /* compare offset distances */
230 >        const void      *p1,
231 >        const void      *p2
232 > )
233   {
234          return(*(const int *)p1 - *(const int *)p2);
235   }
236  
237  
238   int
239 < getclosest(iarr, nc, x, y)      /* get nc closest neighbors on same object */
240 < int     *iarr;
241 < int     nc;
242 < int     x, y;
239 > getclosest(     /* get nc closest neighbors on same object */
240 >        int     *iarr,
241 >        int     nc,
242 >        int     x,
243 >        int     y
244 > )
245   {
246   #define NSCHECK         ((2*SAMPDIST+1)*(2*SAMPDIST+1))
247          static int      hro, vro;
248          static int      ioffs[NSCHECK];
249          OBJECT  myobj;
250          int     i0, nf;
251 <        register int    i, j;
251 >        int     i, j;
252                                          /* get our object number */
253          myobj = obuffer[fndx(x, y)];
254                                          /* special case for borders */
255 <        if ((x < SAMPDIST | x >= hres-SAMPDIST |
256 <                        y < SAMPDIST | y >= vres-SAMPDIST)) {
255 >        if ((x < SAMPDIST) | (x >= hres-SAMPDIST) |
256 >                        (y < SAMPDIST) | (y >= vres-SAMPDIST)) {
257                  int     tndx[NSCHECK][2];
258                  nf = 0;
259                  for (j = y - SAMPDIST; j <= y + SAMPDIST; j++) {
# Line 234 | Line 265 | int    x, y;
265                          i0 = fndx(i, j);
266                          if (!sbuffer[i0])
267                                  continue;
268 <                        if ((myobj != OVOID & obuffer[i0] != myobj))
268 >                        if ((myobj != OVOID) & (obuffer[i0] != myobj))
269                                  continue;
270                          tndx[nf][0] = (i-x)*(i-x) + (j-y)*(j-y);
271                          tndx[nf][1] = i0;
# Line 249 | Line 280 | int    x, y;
280                  return(nf);
281          }
282                                          /* initialize offset array */
283 <        if ((hres != hro | vres != vro)) {
283 >        if ((hres != hro) | (vres != vro)) {
284                  int     toffs[NSCHECK][2];
285                  i0 = fndx(SAMPDIST, SAMPDIST);
286                  nf = 0;
# Line 268 | Line 299 | int    x, y;
299          }
300                                          /* find up to nc neighbors */
301          i0 = fndx(x, y);
302 <        for (j = 0, nf = 0; (j < NSCHECK & nf < nc); j++) {
302 >        for (j = 0, nf = 0; (j < NSCHECK) & (nf < nc); j++) {
303                  i = i0 + ioffs[j];
304 <                if (sbuffer[i] && (myobj == OVOID | obuffer[i] == myobj))
304 >                if (sbuffer[i] && (myobj == OVOID) | (obuffer[i] == myobj))
305                          iarr[nf++] = i;
306          }
307                                          /* return number found */
# Line 280 | Line 311 | int    x, y;
311  
312  
313   static void
314 < setmotion(n, wpos)              /* compute motion vector for this pixel */
315 < register int    n;
316 < FVECT   wpos;
314 > setmotion(              /* compute motion vector for this pixel */
315 >                int     n,
316 >                FVECT   wpos
317 > )
318   {
319          FVECT   ovp;
288        MAT4    xfm;
289        double  d;
320          int     moi;
321          int     xp, yp;
322                                          /* ID object and update maximum HLS */
# Line 306 | Line 336 | FVECT  wpos;
336          yp = (int)(ovp[1]*vres);
337          xmbuffer[n] = xp - (n % hres);
338          ymbuffer[n] = yp - (n / hres);
339 <        if ((xp < 0 | xp >= hres))
339 >        if ((xp < 0) | (xp >= hres))
340                  return;
341 <        if ((yp < 0 | yp >= vres))
341 >        if ((yp < 0) | (yp >= vres))
342                  return;
343          n = fndx(xp, yp);
344 <        if ((zprev[n] < 0.97*ovp[2] | zprev[n] > 1.03*ovp[2]))
344 >        if ((zprev[n] < 0.97*ovp[2]) | (zprev[n] > 1.03*ovp[2]))
345                  oprev[n] = OVOID;       /* assume it's a bad match */
346   }
347  
348  
349   static void
350 < init_frame_sample()             /* sample our initial frame */
350 > init_frame_sample(void)         /* sample our initial frame */
351   {
352          RAY     ir;
353          int     x, y;
354 <        register int    n;
354 >        int     n;
355  
356          if (!silent) {
357                  printf("\tComputing initial samples...");
# Line 332 | Line 362 | init_frame_sample()            /* sample our initial frame */
362              for (x = hres; x--; ) {
363                  double  hv[2];
364                  n = fndx(x, y);
365 <                xmbuffer[n] = MO_UNK;
336 <                ymbuffer[n] = MO_UNK;
365 >                xmbuffer[n] = ymbuffer[n] = MO_UNK;
366                  sample_pos(hv, x, y, 0);
367                  ir.rmax = viewray(ir.rorg, ir.rdir, &vw, hv[0], hv[1]);
368                  if (ir.rmax < -FTINY) {
# Line 344 | Line 373 | init_frame_sample()            /* sample our initial frame */
373                          continue;
374                  }
375                  if (!sample_here(x, y)) {       /* just cast */
376 <                        rayorigin(&ir, NULL, PRIMARY, 1.0);
376 >                        rayorigin(&ir, PRIMARY, NULL, NULL);
377                          if (!localhit(&ir, &thescene)) {
378 <                                if (ir.ro != &Aftplane)
379 <                                        sourcehit(&ir);
378 >                                if (ir.ro != &Aftplane && sourcehit(&ir)) {
379 >                                        rayshade(&ir, ir.ro->omod);
380 >                                        rayparticipate(&ir);
381 >                                }
382                                  copycolor(cbuffer[n], ir.rcol);
383                                  zbuffer[n] = ir.rot;
384                                  obuffer[n] = ir.robj;
# Line 362 | Line 393 | init_frame_sample()            /* sample our initial frame */
393                  }
394                  if (nprocs > 1) {               /* get sample */
395                          int     rval;
396 <                        rayorigin(&ir, NULL, PRIMARY, 1.0);
396 >                        rayorigin(&ir, PRIMARY, NULL, NULL);
397                          ir.rno = n;
398                          rval = ray_pqueue(&ir);
399                          if (!rval)
# Line 376 | Line 407 | init_frame_sample()            /* sample our initial frame */
407                  zbuffer[n] = ir.rot;
408                  obuffer[n] = ir.robj;
409                  sbuffer[n] = 1;
410 <                if (ir.rot >= FHUGE)
410 >                if (ir.rot >= 0.99*FHUGE)
411                          abuffer[n] = ADISTANT;
412                  else {
413                          abuffer[n] = ALOWQ;
# Line 404 | Line 435 | init_frame_sample()            /* sample our initial frame */
435                  n = fndx(x, y);
436                  if ((obj = obuffer[n]) == OVOID)
437                          continue;
438 <                if ((obuffer[n+1] != OVOID & obuffer[n+1] != obj)) {
438 >                if ((obuffer[n+1] != OVOID) & (obuffer[n+1] != obj)) {
439                          obuffer[n] = OVOID;
440                          obuffer[n+1] = OVOID;
441                  }
442 <                if ((obuffer[n+hres] != OVOID & obuffer[n+hres] != obj)) {
442 >                if ((obuffer[n+hres] != OVOID) & (obuffer[n+hres] != obj)) {
443                          obuffer[n] = OVOID;
444                          obuffer[n+hres] = OVOID;
445                  }
# Line 420 | Line 451 | init_frame_sample()            /* sample our initial frame */
451  
452  
453   int
454 < getambcolor(clr, obj)           /* get ambient color for object if we can */
455 < COLOR   clr;
456 < int     obj;
454 > getambcolor(            /* get ambient color for object if we can */
455 >                COLOR   clr,
456 >                int     obj
457 > )
458   {
459 <        register OBJREC *op;
459 >        OBJREC  *op;
460  
461          if (obj == OVOID)
462                  return(0);
463 <        op = objptr(obj);
464 <        if ((op->otype == OBJ_INSTANCE & op->omod == OVOID))
463 >        op = objptr(obj);               /* search for material */
464 >        if (op->omod == OVOID)
465                  return(0);
466 <                                        /* search for material */
467 <        do {
468 <                if (op->omod == OVOID || ofun[op->otype].flags & T_X)
437 <                        return(0);
438 <                op = objptr(op->omod);
439 <        } while (!ismaterial(op->otype));
466 >        op = findmaterial(objptr(op->omod));
467 >        if (op == NULL)
468 >                return(0);
469          /*
470           * Since this routine is called to compute the difference
471           * from rendering with and without interreflections,
# Line 450 | Line 479 | int    obj;
479                          if (lv[0] == op->oname[0] &&
480                                          !strcmp(lv+1, op->oname+1))
481                                  break;
482 <                if ((lv != NULL) != hirendparams.ambincl)
482 >                if ((lv != NULL) ^ hirendparams.ambincl)
483                          return(0);
484          }
485          switch (op->otype) {
# Line 490 | Line 519 | int    obj;
519  
520  
521   double
522 < estimaterr(cs, cs2, ns, ns0)    /* estimate relative error from samples */
523 < COLOR   cs, cs2;
524 < int     ns;
522 > estimaterr(             /* estimate relative error from samples */
523 >                COLOR   cs,
524 >                COLOR   cs2,
525 >                int     ns,
526 >                int ns0
527 > )
528   {
529          double  d, d2, brt;
530  
# Line 513 | Line 545 | int    ns;
545  
546  
547   double
548 < comperr(neigh, nc, ns0)         /* estimate relative error in neighborhood */
549 < int     *neigh;
550 < int     nc;
551 < int     ns0;
548 > comperr(                /* estimate relative error in neighborhood */
549 >                int     *neigh,
550 >                int     nc,
551 >                int     ns0
552 > )
553   {
554          COLOR   csum, csum2;
555          COLOR   ctmp;
556          int     i;
557          int     ns;
558 <        register int    n;
558 >        int     n;
559                                          /* add together samples */
560          setcolor(csum, 0., 0., 0.);
561          setcolor(csum2, 0., 0., 0.);
562 <        for (i = 0, ns = 0; (i < nc & ns < NSAMPOK); i++) {
562 >        for (i = 0, ns = 0; (i < nc) & (ns < NSAMPOK); i++) {
563                  n = neigh[i];
564                  addcolor(csum, cbuffer[n]);
565                  if (val2map != NULL) {
# Line 548 | Line 581 | int    ns0;
581  
582  
583   void
584 < comp_frame_error()              /* initialize frame error values */
584 > comp_frame_error(void)          /* initialize frame error values */
585   {
586 <        BYTE    *edone = NULL;
586 >        uby8    *edone = NULL;
587          COLOR   objamb;
588          double  eest;
589          int     neigh[NSAMPOK];
590          int     nc;
591 <        int     x, y, i, j;
592 <        register int    n;
591 >        int     x, y, i;
592 >        int     n;
593  
594          if (!silent) {
595                  printf("\tComputing error map\n");
# Line 574 | Line 607 | comp_frame_error()             /* initialize frame error values *
607                   * error should be less than the ambient value divided
608                   * by the returned ray value -- we take half of this.
609                   */
610 <                edone = (BYTE *)calloc(hres*vres, sizeof(BYTE));
611 <                for (y = vres; y--; )
612 <                    for (x = hres; x--; ) {
580 <                        n = fndx(x, y);
581 <                        if ((abuffer[n] != ALOWQ | obuffer[n] == OVOID))
610 >                edone = (uby8 *)calloc(hres*vres, sizeof(uby8));
611 >                for (n = hres*vres; n--; ) {
612 >                        if ((abuffer[n] != ALOWQ) | (obuffer[n] == OVOID))
613                                  continue;
614                          if (!getambcolor(objamb, obuffer[n]))
615                                  continue;
# Line 594 | Line 625 | comp_frame_error()             /* initialize frame error values *
625                          else if (i >= ADISTANT/2) i = ADISTANT/2-1;
626                          abuffer[n] = i;
627                          edone[n] = 1;
628 <                    }
628 >                }
629          }
630                                          /* final statistical estimate */
631          for (y = vres; y--; )
# Line 627 | Line 658 | comp_frame_error()             /* initialize frame error values *
658  
659  
660   void
661 < init_frame()                    /* render base (low quality) frame */
661 > init_frame(void)                        /* render base (low quality) frame */
662   {
663          int     restart;
633
664                                          /* allocate/swap buffers */
665          next_frame();
666                                          /* check rendering status */
667          restart = (!nobjects || vdef(MOVE));
668          if (!restart && curparams != &lorendparams && nprocs > 1)
669                  restart = -1;
640        if (restart > 0)
641                if (nprocs > 1)
642                        ray_pdone(1);
643                else
644                        ray_done(1);
670                                          /* post low quality parameters */
671          if (curparams != &lorendparams)
672                  ray_restore(curparams = &lorendparams);
# Line 673 | Line 698 | init_frame()                   /* render base (low quality) frame */
698          init_frame_sample();
699                                          /* initialize frame error */
700          comp_frame_error();
701 < return;
701 > #if 0
702   {
703          float   *ebuf = (float *)malloc(sizeof(float)*hres*vres);
704          char    fnm[256];
705 <        register int    n;
705 >        int     n;
706          for (n = hres*vres; n--; )
707                  ebuf[n] = acctab[abuffer[n]];
708          sprintf(fnm, vval(BASENAME), fcur);
# Line 685 | Line 710 | return;
710          write_map(ebuf, fnm);
711          free((void *)ebuf);
712   }
713 + #endif
714   }
715  
716  
717   void
718 < filter_frame()                  /* interpolation, motion-blur, and exposure */
718 > filter_frame(void)                      /* interpolation, motion-blur, and exposure */
719   {
720 <        double  expval = expspec_val(getexp(fcur));
721 <        int     x, y;
722 <        int     neigh[NPINTERP];
723 <        int     nc;
724 <        COLOR   cval;
725 <        double  w, wsum;
726 <        register int    n;
720 >        const double    expval = expspec_val(getexp(fcur));
721 >        int             x, y;
722 >        int             neigh[NPINTERP];
723 >        int             nc;
724 >        COLOR           cval;
725 >        double          w, wsum;
726 >        int             n;
727  
728   #if 0
729 < /* XXX TEMPORARY!! */
730 < conspicuity();
731 < write_map(cerrmap, "outcmap.pic");
732 < {
733 <        float   *ebuf = (float *)malloc(sizeof(float)*hres*vres);
734 <        for (n = hres*vres; n--; )
735 <                ebuf[n] = acctab[abuffer[n]];
736 <        write_map(ebuf, "outerr.pic");
737 <        free((void *)ebuf);
738 < }
729 >        /* XXX TEMPORARY!! */
730 >        conspicuity();
731 >        write_map(cerrmap, "outcmap.pic");
732 >        {
733 >                float   *ebuf = (float *)malloc(sizeof(float)*hres*vres);
734 >                for (n = hres*vres; n--; )
735 >                        ebuf[n] = acctab[abuffer[n]];
736 >                write_map(ebuf, "outerr.pic");
737 >                free((void *)ebuf);
738 >        }
739   #endif
714
740          if (!silent) {
741                  printf("\tFiltering frame\n");
742                  fflush(stdout);
743          }
744                                          /* normalize samples */
745 <        for (y = vres; y--; )
721 <            for (x = hres; x--; ) {
722 <                n = fndx(x, y);
745 >        for (n = hres*vres; n--; ) {
746                  if (sbuffer[n] <= 1)
747                          continue;
748                  w = 1.0/(double)sbuffer[n];
749                  scalecolor(cbuffer[n], w);
750 <            }
750 >        }
751                                          /* interpolate samples */
752          for (y = vres; y--; )
753              for (x = hres; x--; ) {
# Line 733 | Line 756 | write_map(cerrmap, "outcmap.pic");
756                          continue;
757                  nc = getclosest(neigh, NPINTERP, x, y);
758                  setcolor(cbuffer[n], 0., 0., 0.);
759 +                if (nc <= 0) {          /* no acceptable neighbors */
760 +                        if (y < vres-1)
761 +                                nc = fndx(x, y+1);
762 +                        else if (x < hres-1)
763 +                                nc = fndx(x+1, y);
764 +                        else
765 +                                continue;
766 +                        copycolor(cbuffer[n], cbuffer[nc]);
767 +                        continue;
768 +                }
769                  wsum = 0.;
770                  while (nc-- > 0) {
771                          copycolor(cval, cbuffer[neigh[nc]]);
# Line 742 | Line 775 | write_map(cerrmap, "outcmap.pic");
775                          addcolor(cbuffer[n], cval);
776                          wsum += w;
777                  }
778 <                if (wsum > FTINY) {
779 <                        w = 1.0/wsum;
747 <                        scalecolor(cbuffer[n], w);
748 <                }
778 >                w = 1.0/wsum;
779 >                scalecolor(cbuffer[n], w);
780              }
781                                          /* motion blur if requested */
782          if (mblur > .02) {
752                int     len;
783                  int     xs, ys, xl, yl;
784                  int     rise, run;
785                  long    rise2, run2;
786                  int     n2;
787                  int     cnt;
788                                          /* sum in motion streaks */
789 <                bzero(outbuffer, sizeof(COLOR)*hres*vres);
790 <                bzero(wbuffer, sizeof(float)*hres*vres);
789 >                memset(outbuffer, '\0', sizeof(COLOR)*hres*vres);
790 >                memset(wbuffer, '\0', sizeof(float)*hres*vres);
791                  for (y = vres; y--; )
792                      for (x = hres; x--; ) {
793                          n = fndx(x, y);
# Line 786 | Line 816 | write_map(cerrmap, "outcmap.pic");
816                                  scalecolor(cval, w);
817                                  while (cnt)
818                                          if (rise2 >= run2) {
819 <                                                if ((xl >= 0 & xl < hres &
820 <                                                        yl >= 0 & yl < vres)) {
819 >                                                if ((xl >= 0) & (xl < hres) &
820 >                                                    (yl >= 0) & (yl < vres)) {
821                                                          n2 = fndx(xl, yl);
822                                                          addcolor(outbuffer[n2],
823                                                                          cval);
# Line 807 | Line 837 | write_map(cerrmap, "outcmap.pic");
837                                  scalecolor(cval, w);
838                                  while (cnt)
839                                          if (run2 >= rise2) {
840 <                                                if ((xl >= 0 & xl < hres &
841 <                                                        yl >= 0 & yl < vres)) {
840 >                                                if ((xl >= 0) & (xl < hres) &
841 >                                                    (yl >= 0) & (yl < vres)) {
842                                                          n2 = fndx(xl, yl);
843                                                          addcolor(outbuffer[n2],
844                                                                          cval);
# Line 824 | Line 854 | write_map(cerrmap, "outcmap.pic");
854                          }
855                      }
856                                          /* compute final results */
857 <                for (y = vres; y--; )
828 <                    for (x = hres; x--; ) {
829 <                        n = fndx(x, y);
857 >                for (n = hres*vres; n--; ) {
858                          if (wbuffer[n] <= FTINY)
859                                  continue;
860 <                        w = 1./wbuffer[n];
860 >                        w = expval/wbuffer[n];
861                          scalecolor(outbuffer[n], w);
862 <                    }
863 <        } else
864 <                for (n = hres*vres; n--; )
865 <                        copycolor(outbuffer[n], cbuffer[n]);
838 < /*
839 < for (n = hres*vres; n--; )
840 <        if (!sbuffer[n])
841 <                setcolor(outbuffer[n], 0., 0., 0.);
842 < */
843 <                                /* adjust exposure */
844 <        if ((expval < 0.99 | expval > 1.01))
845 <                for (n = hres*vres; n--; )
862 >                }
863 >        } else {                        /* no blur -- just exposure */
864 >                memcpy(outbuffer, cbuffer, sizeof(COLOR)*hres*vres);
865 >                for (n = ((expval < 0.99) | (expval > 1.01))*hres*vres; n--; )
866                          scalecolor(outbuffer[n], expval);
867 < return;
868 < {
869 <        float   *sbuf = (float *)malloc(sizeof(float)*hres*vres);
870 <        char    fnm[256];
871 <        sprintf(fnm, vval(BASENAME), fcur);
872 <        strcat(fnm, "_outsamp.pic");
873 <        for (n = hres*vres; n--; )
874 <                sbuf[n] = (float)sbuffer[n];
875 <        write_map(sbuf, fnm);
876 <        free((void *)sbuf);
867 >        }
868 >        /*
869 >           for (n = hres*vres; n--; )
870 >                   if (!sbuffer[n])
871 >                           setcolor(outbuffer[n], 0., 0., 0.);
872 >         */
873 > #if 0
874 >        {
875 >                float   *sbuf = (float *)malloc(sizeof(float)*hres*vres);
876 >                char    fnm[256];
877 >                sprintf(fnm, vval(BASENAME), fcur);
878 >                strcat(fnm, "_outsamp.pic");
879 >                for (n = hres*vres; n--; )
880 >                        sbuf[n] = (float)sbuffer[n];
881 >                write_map(sbuf, fnm);
882 >                free((void *)sbuf);
883 >        }
884 > #endif
885   }
858 }
886  
887  
888   void
889 < send_frame()                    /* send frame to destination */
889 > send_frame(void)                        /* send frame to destination */
890   {
891 <        char    pfname[1024];
891 >        char    fname[1024];
892          double  d;
893          FILE    *fp;
894          int     y;
895                                          /* open output picture */
896 <        sprintf(pfname, vval(BASENAME), fcur);
897 <        strcat(pfname, ".pic");
898 <        fp = fopen(pfname, "w");
896 >        sprintf(fname, vval(BASENAME), fcur);
897 >        strcat(fname, ".hdr");
898 >        fp = fopen(fname, "w");
899          if (fp == NULL) {
900 <                sprintf(errmsg, "cannot open output frame \"%s\"", pfname);
900 >                sprintf(errmsg, "cannot open output frame \"%s\"", fname);
901                  error(SYSTEM, errmsg);
902          }
903 < #ifdef MSDOS
877 <        setmode(fileno(fp), O_BINARY);
878 < #endif
903 >        SET_FILE_BINARY(fp);
904          if (!silent) {
905 <                printf("\tWriting to \"%s\"\n", pfname);
905 >                printf("\tWriting to \"%s\"\n", fname);
906                  fflush(stdout);
907          }
908                                          /* write header */
# Line 888 | Line 913 | send_frame()                   /* send frame to destination */
913          fputnow(fp);
914          fputs(VIEWSTR, fp); fprintview(&vw, fp); fputc('\n', fp);
915          d = expspec_val(getexp(fcur));
916 <        if ((d < 0.99 | d > 1.01))
916 >        if ((d < 0.99) | (d > 1.01))
917                  fputexpos(d, fp);
918          d = viewaspect(&vw) * hres / vres;
919 <        if ((d < 0.99 | d > 1.01))
919 >        if ((d < 0.99) | (d > 1.01))
920                  fputaspect(d, fp);
921          fputformat(COLRFMT, fp);
922          fputc('\n', fp);                /* end header */
# Line 906 | Line 931 | send_frame()                   /* send frame to destination */
931                          goto writerr;
932          if (fclose(fp) == EOF)
933                  goto writerr;
934 +        if (vdef(ZNAME)) {              /* output z-buffer */
935 +                sprintf(fname, vval(ZNAME), fcur);
936 +                strcat(fname, ".zbf");
937 +                fp = fopen(fname, "w");
938 +                if (fp == NULL) {
939 +                        sprintf(errmsg, "cannot open z-file \"%s\"\n", fname);
940 +                        error(SYSTEM, errmsg);
941 +                }
942 +                SET_FILE_BINARY(fp);
943 +                if (!silent) {
944 +                        printf("\tWriting depths to \"%s\"\n", fname);
945 +                        fflush(stdout);
946 +                }
947 +                for (y = vres; y--; )
948 +                        if (fwrite(zbuffer+y*hres, sizeof(float),
949 +                                                hres, fp) != hres)
950 +                                goto writerr;
951 +                if (fclose(fp) == EOF)
952 +                        goto writerr;
953 +        }
954 +        if (vdef(MNAME)) {              /* output motion buffer */
955 +                unsigned short  *mbuffer = (unsigned short *)malloc(
956 +                                                sizeof(unsigned short)*3*hres);
957 +                int             x, n;
958 +                if (mbuffer == NULL)
959 +                        error(SYSTEM, "out of memory in send_frame");
960 +                sprintf(fname, vval(MNAME), fcur);
961 +                strcat(fname, ".mvo");
962 +                fp = fopen(fname, "w");
963 +                if (fp == NULL) {
964 +                        sprintf(errmsg, "cannot open motion file \"%s\"\n",
965 +                                                        fname);
966 +                        error(SYSTEM, errmsg);
967 +                }
968 +                SET_FILE_BINARY(fp);
969 +                if (!silent) {
970 +                        printf("\tWriting motion vectors to \"%s\"\n", fname);
971 +                        fflush(stdout);
972 +                }
973 +                for (y = vres; y--; ) {
974 +                        for (x = hres; x--; ) {
975 +                                n = fndx(x,y);
976 +                                mbuffer[3*x] = xmbuffer[n] + 0x8000;
977 +                                mbuffer[3*x+1] = ymbuffer[n] + 0x8000;
978 +                                mbuffer[3*x+2] = (oprev[n]!=OVOID)*0x8000;
979 +                        }
980 +                        if (fwrite(mbuffer, sizeof(*mbuffer),
981 +                                                3*hres, fp) != 3*hres)
982 +                                goto writerr;
983 +                }
984 +                free((void *)mbuffer);
985 +                if (fclose(fp) == EOF)
986 +                        goto writerr;
987 +        }
988          return;                         /* all is well */
989   writerr:
990 <        sprintf(errmsg, "error writing frame \"%s\"", pfname);
990 >        sprintf(errmsg, "error writing file \"%s\"", fname);
991          error(SYSTEM, errmsg);
992   }
993  
994  
995   void
996 < free_frame()                    /* free frame allocation */
996 > free_frame(void)                        /* free frame allocation */
997   {
998          if (cbuffer == NULL)
999                  return;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines