--- ray/src/util/ranimove1.c 2010/11/05 04:36:24 3.16 +++ ray/src/util/ranimove1.c 2012/09/30 19:18:41 3.19 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: ranimove1.c,v 3.16 2010/11/05 04:36:24 greg Exp $"; +static const char RCSid[] = "$Id: ranimove1.c,v 3.19 2012/09/30 19:18:41 greg Exp $"; #endif /* * ranimove1.c @@ -30,14 +30,14 @@ float *zbuffer; /* depth at each pixel */ OBJECT *obuffer; /* object id at each pixel */ short *xmbuffer; /* x motion at each pixel */ short *ymbuffer; /* y motion at each pixel */ -BYTE *abuffer; /* accuracy at each pixel */ -BYTE *sbuffer; /* sample count per pixel */ +uby8 *abuffer; /* accuracy at each pixel */ +uby8 *sbuffer; /* sample count per pixel */ VIEW vwprev; /* last frame's view */ COLOR *cprev; /* last frame colors */ float *zprev; /* last frame depth */ OBJECT *oprev; /* last frame objects */ -BYTE *aprev; /* last frame accuracy */ +uby8 *aprev; /* last frame accuracy */ float *cerrmap; /* conspicuous error map */ COLOR *val2map; /* value-squared map for variance */ @@ -55,7 +55,7 @@ static void init_frame_sample(void); #if 0 -extern void +void write_map( /* write out float map (debugging) */ float *mp, char *fn @@ -131,12 +131,12 @@ next_frame(void) /* prepare next frame buffer */ obuffer = (OBJECT *)malloc(sizeof(OBJECT)*hres*vres); xmbuffer = (short *)malloc(sizeof(short)*hres*vres); ymbuffer = (short *)malloc(sizeof(short)*hres*vres); - abuffer = (BYTE *)calloc(hres*vres, sizeof(BYTE)); - sbuffer = (BYTE *)calloc(hres*vres, sizeof(BYTE)); + abuffer = (uby8 *)calloc(hres*vres, sizeof(uby8)); + sbuffer = (uby8 *)calloc(hres*vres, sizeof(uby8)); cprev = (COLOR *)malloc(sizeof(COLOR)*hres*vres); zprev = (float *)malloc(sizeof(float)*hres*vres); oprev = (OBJECT *)malloc(sizeof(OBJECT)*hres*vres); - aprev = (BYTE *)malloc(sizeof(BYTE)*hres*vres); + aprev = (uby8 *)malloc(sizeof(uby8)*hres*vres); if ((cbuffer==NULL) | (zbuffer==NULL) | (obuffer==NULL) | (xmbuffer==NULL) | (ymbuffer==NULL) | (abuffer==NULL) | (sbuffer==NULL) | @@ -145,6 +145,7 @@ next_frame(void) /* prepare next frame buffer */ error(SYSTEM, "out of memory in init_frame"); for (n = hres*vres; n--; ) { zprev[n] = -1.f; + xmbuffer[n] = ymbuffer[n] = MO_UNK; oprev[n] = OVOID; } frm_stop = getTime() + rtperfrm; @@ -152,13 +153,13 @@ next_frame(void) /* prepare next frame buffer */ COLOR *cp; /* else just swap buffers */ float *fp; OBJECT *op; - BYTE *bp; + uby8 *bp; cp = cprev; cprev = cbuffer; cbuffer = cp; fp = zprev; zprev = zbuffer; zbuffer = fp; op = oprev; oprev = obuffer; obuffer = op; bp = aprev; aprev = abuffer; abuffer = bp; - memset(abuffer, '\0', sizeof(BYTE)*hres*vres); - memset(sbuffer, '\0', sizeof(BYTE)*hres*vres); + memset(abuffer, '\0', sizeof(uby8)*hres*vres); + memset(sbuffer, '\0', sizeof(uby8)*hres*vres); frm_stop += rtperfrm; } cerrmap = NULL; @@ -172,8 +173,8 @@ next_frame(void) /* prepare next frame buffer */ static int sample_here( /* 4x4 quincunx sample at this pixel? */ - register int x, - register int y + int x, + int y ) { if (y & 0x1) /* every other row has samples */ @@ -184,7 +185,7 @@ sample_here( /* 4x4 quincunx sample at this pixel? */ } -extern void +void sample_pos( /* compute jittered sample position */ double hv[2], int x, @@ -201,7 +202,7 @@ sample_pos( /* compute jittered sample position */ } -extern double +double sample_wt( /* compute interpolant sample weight */ int xo, int yo @@ -235,7 +236,7 @@ offset_cmp( /* compare offset distances */ } -extern int +int getclosest( /* get nc closest neighbors on same object */ int *iarr, int nc, @@ -248,7 +249,7 @@ getclosest( /* get nc closest neighbors on same object static int ioffs[NSCHECK]; OBJECT myobj; int i0, nf; - register int i, j; + int i, j; /* get our object number */ myobj = obuffer[fndx(x, y)]; /* special case for borders */ @@ -312,7 +313,7 @@ getclosest( /* get nc closest neighbors on same object static void setmotion( /* compute motion vector for this pixel */ - register int n, + int n, FVECT wpos ) { @@ -351,7 +352,7 @@ init_frame_sample(void) /* sample our initial frame * { RAY ir; int x, y; - register int n; + int n; if (!silent) { printf("\tComputing initial samples..."); @@ -451,13 +452,13 @@ init_frame_sample(void) /* sample our initial frame * } -extern int +int getambcolor( /* get ambient color for object if we can */ COLOR clr, int obj ) { - register OBJREC *op; + OBJREC *op; if (obj == OVOID) return(0); @@ -519,7 +520,7 @@ getambcolor( /* get ambient color for object if we ca } -extern double +double estimaterr( /* estimate relative error from samples */ COLOR cs, COLOR cs2, @@ -545,7 +546,7 @@ estimaterr( /* estimate relative error from samples * } -extern double +double comperr( /* estimate relative error in neighborhood */ int *neigh, int nc, @@ -556,7 +557,7 @@ comperr( /* estimate relative error in neighborhood * COLOR ctmp; int i; int ns; - register int n; + int n; /* add together samples */ setcolor(csum, 0., 0., 0.); setcolor(csum2, 0., 0., 0.); @@ -581,16 +582,16 @@ comperr( /* estimate relative error in neighborhood * } -extern void +void comp_frame_error(void) /* initialize frame error values */ { - BYTE *edone = NULL; + uby8 *edone = NULL; COLOR objamb; double eest; int neigh[NSAMPOK]; int nc; int x, y, i; - register int n; + int n; if (!silent) { printf("\tComputing error map\n"); @@ -608,10 +609,8 @@ comp_frame_error(void) /* initialize frame error valu * error should be less than the ambient value divided * by the returned ray value -- we take half of this. */ - edone = (BYTE *)calloc(hres*vres, sizeof(BYTE)); - for (y = vres; y--; ) - for (x = hres; x--; ) { - n = fndx(x, y); + edone = (uby8 *)calloc(hres*vres, sizeof(uby8)); + for (n = hres*vres; n--; ) { if ((abuffer[n] != ALOWQ) | (obuffer[n] == OVOID)) continue; if (!getambcolor(objamb, obuffer[n])) @@ -628,7 +627,7 @@ comp_frame_error(void) /* initialize frame error valu else if (i >= ADISTANT/2) i = ADISTANT/2-1; abuffer[n] = i; edone[n] = 1; - } + } } /* final statistical estimate */ for (y = vres; y--; ) @@ -660,7 +659,7 @@ comp_frame_error(void) /* initialize frame error valu } -extern void +void init_frame(void) /* render base (low quality) frame */ { int restart; @@ -705,7 +704,7 @@ init_frame(void) /* render base (low quality) frame { float *ebuf = (float *)malloc(sizeof(float)*hres*vres); char fnm[256]; - register int n; + int n; for (n = hres*vres; n--; ) ebuf[n] = acctab[abuffer[n]]; sprintf(fnm, vval(BASENAME), fcur); @@ -717,16 +716,16 @@ init_frame(void) /* render base (low quality) frame } -extern void +void filter_frame(void) /* interpolation, motion-blur, and exposure */ { - double expval = expspec_val(getexp(fcur)); - int x, y; - int neigh[NPINTERP]; - int nc; - COLOR cval; - double w, wsum; - register int n; + const double expval = expspec_val(getexp(fcur)); + int x, y; + int neigh[NPINTERP]; + int nc; + COLOR cval; + double w, wsum; + int n; #if 0 /* XXX TEMPORARY!! */ @@ -746,14 +745,12 @@ filter_frame(void) /* interpolation, motion-blur, an fflush(stdout); } /* normalize samples */ - for (y = vres; y--; ) - for (x = hres; x--; ) { - n = fndx(x, y); + for (n = hres*vres; n--; ) { if (sbuffer[n] <= 1) continue; w = 1.0/(double)sbuffer[n]; scalecolor(cbuffer[n], w); - } + } /* interpolate samples */ for (y = vres; y--; ) for (x = hres; x--; ) { @@ -860,26 +857,22 @@ filter_frame(void) /* interpolation, motion-blur, an } } /* compute final results */ - for (y = vres; y--; ) - for (x = hres; x--; ) { - n = fndx(x, y); + for (n = hres*vres; n--; ) { if (wbuffer[n] <= FTINY) continue; - w = 1./wbuffer[n]; + w = expval/wbuffer[n]; scalecolor(outbuffer[n], w); - } - } else - for (n = hres*vres; n--; ) - copycolor(outbuffer[n], cbuffer[n]); + } + } else { /* no blur -- just exposure */ + memcpy(outbuffer, cbuffer, sizeof(COLOR)*hres*vres); + for (n = ((expval < 0.99) | (expval > 1.01))*hres*vres; n--; ) + scalecolor(outbuffer[n], expval); + } /* for (n = hres*vres; n--; ) if (!sbuffer[n]) setcolor(outbuffer[n], 0., 0., 0.); */ - /* adjust exposure */ - if ((expval < 0.99) | (expval > 1.01)) - for (n = hres*vres; n--; ) - scalecolor(outbuffer[n], expval); #if 0 { float *sbuf = (float *)malloc(sizeof(float)*hres*vres); @@ -895,24 +888,24 @@ filter_frame(void) /* interpolation, motion-blur, an } -extern void +void send_frame(void) /* send frame to destination */ { - char pfname[1024]; + char fname[1024]; double d; FILE *fp; int y; /* open output picture */ - sprintf(pfname, vval(BASENAME), fcur); - strcat(pfname, ".hdr"); - fp = fopen(pfname, "w"); + sprintf(fname, vval(BASENAME), fcur); + strcat(fname, ".hdr"); + fp = fopen(fname, "w"); if (fp == NULL) { - sprintf(errmsg, "cannot open output frame \"%s\"", pfname); + sprintf(errmsg, "cannot open output frame \"%s\"", fname); error(SYSTEM, errmsg); } SET_FILE_BINARY(fp); if (!silent) { - printf("\tWriting to \"%s\"\n", pfname); + printf("\tWriting to \"%s\"\n", fname); fflush(stdout); } /* write header */ @@ -941,14 +934,67 @@ send_frame(void) /* send frame to destination */ goto writerr; if (fclose(fp) == EOF) goto writerr; + if (vdef(ZNAME)) { /* output z-buffer */ + sprintf(fname, vval(ZNAME), fcur); + strcat(fname, ".zbf"); + fp = fopen(fname, "w"); + if (fp == NULL) { + sprintf(errmsg, "cannot open z-file \"%s\"\n", fname); + error(SYSTEM, errmsg); + } + SET_FILE_BINARY(fp); + if (!silent) { + printf("\tWriting depths to \"%s\"\n", fname); + fflush(stdout); + } + for (y = vres; y--; ) + if (fwrite(zbuffer+y*hres, sizeof(float), + hres, fp) != hres) + goto writerr; + if (fclose(fp) == EOF) + goto writerr; + } + if (vdef(MNAME)) { /* output motion buffer */ + unsigned short *mbuffer = (unsigned short *)malloc( + sizeof(unsigned short)*3*hres); + int x; + if (mbuffer == NULL) + error(SYSTEM, "out of memory in send_frame()"); + sprintf(fname, vval(MNAME), fcur); + strcat(fname, ".mvo"); + fp = fopen(fname, "w"); + if (fp == NULL) { + sprintf(errmsg, "cannot open motion file \"%s\"\n", + fname); + error(SYSTEM, errmsg); + } + SET_FILE_BINARY(fp); + if (!silent) { + printf("\tWriting motion vectors to \"%s\"\n", fname); + fflush(stdout); + } + for (y = vres; y--; ) { + for (x = hres; x--; ) { + mbuffer[3*x] = xmbuffer[fndx(x,y)] + 0x8000; + mbuffer[3*x+1] = ymbuffer[fndx(x,y)] + 0x8000; + mbuffer[3*x+2] = (oprev[fndx(x,y)]!=OVOID)*0x8000; + } + if (fwrite(mbuffer, sizeof(*mbuffer), + 3*hres, fp) != 3*hres) + goto writerr; + } + free((void *)mbuffer); + if (fclose(fp) == EOF) + goto writerr; + } return; /* all is well */ writerr: - sprintf(errmsg, "error writing frame \"%s\"", pfname); + sprintf(errmsg, "error writing file \"%s\"", fname); error(SYSTEM, errmsg); } -extern void +void free_frame(void) /* free frame allocation */ { if (cbuffer == NULL)