--- ray/src/util/ranimove1.c 2005/12/20 03:49:51 3.12 +++ ray/src/util/ranimove1.c 2012/10/05 00:59:38 3.21 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: ranimove1.c,v 3.12 2005/12/20 03:49:51 greg Exp $"; +static const char RCSid[] = "$Id: ranimove1.c,v 3.21 2012/10/05 00:59:38 greg Exp $"; #endif /* * ranimove1.c @@ -30,14 +30,15 @@ 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 */ +COLOR *outbuffer; /* output buffer (may equal cbuffer) */ 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 */ @@ -48,13 +49,14 @@ double hlsmax; /* maximum high-level saliency this f static void next_frame(void); -static int sample_here(int x, int y); -static int offset_cmp(const void *p1, const void *p2); -static void setmotion(int n, FVECT wpos); +static int sample_here(int x, int y); +static int offset_cmp(const void *p1, const void *p2); +static void setmotion(int n, FVECT wpos); static void init_frame_sample(void); -extern void +#if 0 +void write_map( /* write out float map (debugging) */ float *mp, char *fn @@ -79,6 +81,7 @@ write_map( /* write out float map (debugging) */ } fclose(fp); } +#endif static void @@ -105,7 +108,7 @@ next_frame(void) /* prepare next frame buffer */ error(USER, errmsg); } if (cbuffer == NULL) { - /* compute resolution and allocate */ + int n; /* compute resolution and allocate */ switch (sscanf(vval(RESOLUTION), "%d %d %lf", &hres, &vres, &pixaspect)) { case 1: @@ -128,30 +131,44 @@ 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 (mblur > .02) + outbuffer = (COLOR *)malloc(sizeof(COLOR)*hres*vres); + else + outbuffer = cbuffer; if ((cbuffer==NULL) | (zbuffer==NULL) | (obuffer==NULL) | (xmbuffer==NULL) | (ymbuffer==NULL) | (abuffer==NULL) | (sbuffer==NULL) | (cprev==NULL) | (zprev == NULL) | - (oprev==NULL) | (aprev==NULL)) + (oprev==NULL) | (aprev==NULL) | + (outbuffer==NULL)) error(SYSTEM, "out of memory in init_frame"); + for (n = hres*vres; n--; ) { + zprev[n] = -1.f; + oprev[n] = OVOID; + } frm_stop = getTime() + rtperfrm; - } else { - COLOR *cp; /* else just swap buffers */ + } else { /* else just swap buffers */ float *fp; OBJECT *op; - BYTE *bp; - cp = cprev; cprev = cbuffer; cbuffer = cp; + uby8 *bp; + if (outbuffer != cbuffer) { + COLOR *cp = cprev; + cprev = cbuffer; cbuffer = cp; + } else { + outbuffer = cprev; cprev = cbuffer; + cbuffer = outbuffer; + } 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; @@ -165,8 +182,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 */ @@ -177,7 +194,7 @@ sample_here( /* 4x4 quincunx sample at this pixel? */ } -extern void +void sample_pos( /* compute jittered sample position */ double hv[2], int x, @@ -194,7 +211,7 @@ sample_pos( /* compute jittered sample position */ } -extern double +double sample_wt( /* compute interpolant sample weight */ int xo, int yo @@ -228,7 +245,7 @@ offset_cmp( /* compare offset distances */ } -extern int +int getclosest( /* get nc closest neighbors on same object */ int *iarr, int nc, @@ -241,7 +258,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 */ @@ -305,7 +322,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 ) { @@ -344,7 +361,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..."); @@ -355,8 +372,7 @@ init_frame_sample(void) /* sample our initial frame * for (x = hres; x--; ) { double hv[2]; n = fndx(x, y); - xmbuffer[n] = MO_UNK; - ymbuffer[n] = MO_UNK; + xmbuffer[n] = ymbuffer[n] = MO_UNK; sample_pos(hv, x, y, 0); ir.rmax = viewray(ir.rorg, ir.rdir, &vw, hv[0], hv[1]); if (ir.rmax < -FTINY) { @@ -369,8 +385,10 @@ init_frame_sample(void) /* sample our initial frame * if (!sample_here(x, y)) { /* just cast */ rayorigin(&ir, PRIMARY, NULL, NULL); if (!localhit(&ir, &thescene)) { - if (ir.ro != &Aftplane) - sourcehit(&ir); + if (ir.ro != &Aftplane && sourcehit(&ir)) { + rayshade(&ir, ir.ro->omod); + rayparticipate(&ir); + } copycolor(cbuffer[n], ir.rcol); zbuffer[n] = ir.rot; obuffer[n] = ir.robj; @@ -399,7 +417,7 @@ init_frame_sample(void) /* sample our initial frame * zbuffer[n] = ir.rot; obuffer[n] = ir.robj; sbuffer[n] = 1; - if (ir.rot >= FHUGE) + if (ir.rot >= 0.99*FHUGE) abuffer[n] = ADISTANT; else { abuffer[n] = ALOWQ; @@ -442,25 +460,22 @@ 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); - op = objptr(obj); - if ((op->otype == OBJ_INSTANCE) & (op->omod == OVOID)) + op = objptr(obj); /* search for material */ + if (op->omod == OVOID) return(0); - /* search for material */ - do { - if (op->omod == OVOID || ofun[op->otype].flags & T_X) - return(0); - op = objptr(op->omod); - } while (!ismaterial(op->otype)); + op = findmaterial(objptr(op->omod)); + if (op == NULL) + return(0); /* * Since this routine is called to compute the difference * from rendering with and without interreflections, @@ -474,7 +489,7 @@ getambcolor( /* get ambient color for object if we ca if (lv[0] == op->oname[0] && !strcmp(lv+1, op->oname+1)) break; - if ((lv != NULL) != hirendparams.ambincl) + if ((lv != NULL) ^ hirendparams.ambincl) return(0); } switch (op->otype) { @@ -513,7 +528,7 @@ getambcolor( /* get ambient color for object if we ca } -extern double +double estimaterr( /* estimate relative error from samples */ COLOR cs, COLOR cs2, @@ -539,7 +554,7 @@ estimaterr( /* estimate relative error from samples * } -extern double +double comperr( /* estimate relative error in neighborhood */ int *neigh, int nc, @@ -550,7 +565,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.); @@ -564,10 +579,8 @@ comperr( /* estimate relative error in neighborhood * } if (sbuffer[n] != 1) error(CONSISTENCY, "bad count in comperr"); - setcolor(ctmp, - colval(cbuffer[n],RED)*colval(cbuffer[n],RED), - colval(cbuffer[n],GRN)*colval(cbuffer[n],GRN), - colval(cbuffer[n],BLU)*colval(cbuffer[n],BLU)); + copycolor(ctmp, cbuffer[n]); + multcolor(ctmp, ctmp); addcolor(csum2, ctmp); ns++; } @@ -575,16 +588,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"); @@ -602,10 +615,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])) @@ -622,7 +633,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--; ) @@ -654,23 +665,16 @@ comp_frame_error(void) /* initialize frame error valu } -extern void +void init_frame(void) /* render base (low quality) frame */ { int restart; - /* allocate/swap buffers */ next_frame(); /* check rendering status */ restart = (!nobjects || vdef(MOVE)); if (!restart && curparams != &lorendparams && nprocs > 1) restart = -1; - if (restart > 0) { - if (nprocs > 1) - ray_pdone(1); - else - ray_done(1); - } /* post low quality parameters */ if (curparams != &lorendparams) ray_restore(curparams = &lorendparams); @@ -702,11 +706,11 @@ init_frame(void) /* render base (low quality) frame init_frame_sample(); /* initialize frame error */ comp_frame_error(); -return; +#if 0 { 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); @@ -714,19 +718,20 @@ return; write_map(ebuf, fnm); free((void *)ebuf); } +#endif } -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!! */ @@ -740,20 +745,17 @@ filter_frame(void) /* interpolation, motion-blur, an free((void *)ebuf); } #endif - if (!silent) { printf("\tFiltering frame\n"); 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--; ) { @@ -785,15 +787,17 @@ filter_frame(void) /* interpolation, motion-blur, an scalecolor(cbuffer[n], w); } /* motion blur if requested */ - if (mblur > .02) { + if (outbuffer != cbuffer) { int xs, ys, xl, yl; int rise, run; long rise2, run2; int n2; int cnt; +fprintf(stderr, "outbuffer=0x%lx, cbuffer=0x%lx, cprev=0x%lx\n", +(size_t)outbuffer, (size_t)cbuffer, (size_t)cprev); /* sum in motion streaks */ - memset(outbuffer, '\0', sizeof(COLOR)*hres*vres); - memset(wbuffer, '\0', sizeof(float)*hres*vres); + memset(outbuffer, 0, sizeof(COLOR)*hres*vres); + memset(wbuffer, 0, sizeof(float)*hres*vres); for (y = vres; y--; ) for (x = hres; x--; ) { n = fndx(x, y); @@ -808,8 +812,7 @@ filter_frame(void) /* interpolation, motion-blur, an wbuffer[n] += 1.; continue; } - xl = x - run/4; - yl = y - rise/4; + xl = x; yl = y; if (run < 0) { xs = -1; run = -run; } else xs = 1; if (rise < 0) { ys = -1; rise = -rise; } @@ -860,26 +863,25 @@ filter_frame(void) /* interpolation, motion-blur, an } } /* compute final results */ - for (y = vres; y--; ) - for (x = hres; x--; ) { - n = fndx(x, y); - if (wbuffer[n] <= FTINY) - continue; - w = 1./wbuffer[n]; - scalecolor(outbuffer[n], w); - } - } else for (n = hres*vres; n--; ) - copycolor(outbuffer[n], cbuffer[n]); + if (wbuffer[n] > 1.02) { + w = 1./wbuffer[n]; + scalecolor(outbuffer[n], w); + } else if (wbuffer[n] < 0.98) { + w = 1.-wbuffer[n]; + copycolor(cval, cprev[n]); + scalecolor(cval, w); + addcolor(outbuffer[n], cval); + } + } + /* adjust exposure */ + for (n = ((expval < 0.98) | (expval > 1.02))*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 +897,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, ".pic"); - 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,18 +943,75 @@ 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, n; + 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--; ) { + n = fndx(x,y); + mbuffer[3*x] = xmbuffer[n] + 0x8000; + mbuffer[3*x+1] = ymbuffer[n] + 0x8000; + mbuffer[3*x+2] = (oprev[n]!=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) return; + if (outbuffer != cbuffer) + free((void *)outbuffer); + outbuffer = NULL; free((void *)cbuffer); cbuffer = NULL; free((void *)zbuffer); zbuffer = NULL; free((void *)obuffer); obuffer = NULL;