--- ray/src/util/ranimove1.c 2003/02/25 02:47:24 3.2 +++ ray/src/util/ranimove1.c 2005/12/20 03:49:51 3.12 @@ -1,18 +1,22 @@ #ifndef lint -static const char RCSid[] = "$Id: ranimove1.c,v 3.2 2003/02/25 02:47:24 greg Exp $"; +static const char RCSid[] = "$Id: ranimove1.c,v 3.12 2005/12/20 03:49:51 greg Exp $"; #endif /* * ranimove1.c * - * Basic frame rendering routines for ranimate(1). + * Basic frame rendering routines for ranimove(1). * * Created by Gregory Ward on Wed Jan 08 2003. */ #include "copyright.h" +#include + +#include "platform.h" #include "ranimove.h" #include "otypes.h" +#include "source.h" #include "random.h" double acctab[256]; /* accuracy value table */ @@ -43,10 +47,18 @@ double frm_stop; /* when to stop rendering this frame double hlsmax; /* maximum high-level saliency this frame */ -void -write_map(mp, fn) /* write out float map (debugging) */ -float *mp; -char *fn; +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 void init_frame_sample(void); + + +extern void +write_map( /* write out float map (debugging) */ + float *mp, + char *fn +) { FILE *fp = fopen(fn, "w"); COLOR scanbuf[2048]; @@ -70,15 +82,15 @@ char *fn; static void -next_frame() /* prepare next frame buffer */ +next_frame(void) /* prepare next frame buffer */ { VIEW *fv; char *err; /* get previous view */ if (vw.type != 0) - copystruct(&vwprev, &vw); + vwprev = vw; else if (fcur > 1 && (fv = getview(fcur-1)) != NULL) { - copystruct(&vwprev, fv); + vwprev = *fv; if (setview(&vwprev) != NULL) vwprev.type = 0; } @@ -87,7 +99,7 @@ next_frame() /* prepare next frame buffer */ sprintf(errmsg, "cannot get view for frame %d", fcur); error(USER, errmsg); } - copystruct(&vw, fv); + vw = *fv; if ((err = setview(&vw)) != NULL) { sprintf(errmsg, "view error at frame %d: %s", fcur, err); error(USER, errmsg); @@ -103,7 +115,7 @@ next_frame() /* prepare next frame buffer */ pixaspect = 1.; /* fall through */ case 3: - if ((hres > 0 & vres > 0)) + if ((hres > 0) & (vres > 0)) break; /* fall through */ default: @@ -122,11 +134,11 @@ next_frame() /* prepare next frame buffer */ zprev = (float *)malloc(sizeof(float)*hres*vres); oprev = (OBJECT *)malloc(sizeof(OBJECT)*hres*vres); aprev = (BYTE *)malloc(sizeof(BYTE)*hres*vres); - if ((cbuffer==NULL | zbuffer==NULL | obuffer==NULL | - xmbuffer==NULL | ymbuffer==NULL | - abuffer==NULL | sbuffer==NULL | - cprev==NULL | zprev == NULL | - oprev==NULL | aprev==NULL)) + if ((cbuffer==NULL) | (zbuffer==NULL) | (obuffer==NULL) | + (xmbuffer==NULL) | (ymbuffer==NULL) | + (abuffer==NULL) | (sbuffer==NULL) | + (cprev==NULL) | (zprev == NULL) | + (oprev==NULL) | (aprev==NULL)) error(SYSTEM, "out of memory in init_frame"); frm_stop = getTime() + rtperfrm; } else { @@ -138,8 +150,8 @@ next_frame() /* prepare next frame buffer */ fp = zprev; zprev = zbuffer; zbuffer = fp; op = oprev; oprev = obuffer; obuffer = op; bp = aprev; aprev = abuffer; abuffer = bp; - bzero(abuffer, sizeof(BYTE)*hres*vres); - bzero(sbuffer, sizeof(BYTE)*hres*vres); + memset(abuffer, '\0', sizeof(BYTE)*hres*vres); + memset(sbuffer, '\0', sizeof(BYTE)*hres*vres); frm_stop += rtperfrm; } cerrmap = NULL; @@ -152,8 +164,10 @@ next_frame() /* prepare next frame buffer */ static int -sample_here(x, y) /* 4x4 quincunx sample at this pixel? */ -register int x, y; +sample_here( /* 4x4 quincunx sample at this pixel? */ + register int x, + register int y +) { if (y & 0x1) /* every other row has samples */ return(0); @@ -163,11 +177,13 @@ register int x, y; } -void -sample_pos(hv, x, y, sn) /* compute jittered sample position */ -double hv[2]; -int x, y; -int sn; +extern void +sample_pos( /* compute jittered sample position */ + double hv[2], + int x, + int y, + int sn +) { int hl[2]; @@ -178,12 +194,16 @@ int sn; } -double -sample_wt(xo, yo) /* compute interpolant sample weight */ -int xo, yo; +extern double +sample_wt( /* compute interpolant sample weight */ + int xo, + int yo +) { static double etab[400]; - int rad2 = xo*xo + yo*yo; + /* we can't use the name rad2 here, for some reason Visual C + thinks that is a constant (compiler bug?) */ + int rad_2 = xo*xo + yo*yo; int i; if (etab[0] <= FTINY) /* initialize exponent table */ @@ -191,7 +211,7 @@ int xo, yo; etab[i] = exp(-0.1*i); /* look up Gaussian */ - i = (int)((10.*3./(double)SAMPDIST2)*rad2 + .5); + i = (int)((10.*3./(double)SAMPDIST2)*rad_2 + .5); if (i >= 400) return(0.0); return(etab[i]); @@ -199,18 +219,22 @@ int xo, yo; static int -offset_cmp(p1, p2) /* compare offset distances */ -const void *p1, *p2; +offset_cmp( /* compare offset distances */ + const void *p1, + const void *p2 +) { return(*(const int *)p1 - *(const int *)p2); } -int -getclosest(iarr, nc, x, y) /* get nc closest neighbors on same object */ -int *iarr; -int nc; -int x, y; +extern int +getclosest( /* get nc closest neighbors on same object */ + int *iarr, + int nc, + int x, + int y +) { #define NSCHECK ((2*SAMPDIST+1)*(2*SAMPDIST+1)) static int hro, vro; @@ -221,8 +245,8 @@ int x, y; /* get our object number */ myobj = obuffer[fndx(x, y)]; /* special case for borders */ - if ((x < SAMPDIST | x >= hres-SAMPDIST | - y < SAMPDIST | y >= vres-SAMPDIST)) { + if ((x < SAMPDIST) | (x >= hres-SAMPDIST) | + (y < SAMPDIST) | (y >= vres-SAMPDIST)) { int tndx[NSCHECK][2]; nf = 0; for (j = y - SAMPDIST; j <= y + SAMPDIST; j++) { @@ -234,7 +258,7 @@ int x, y; i0 = fndx(i, j); if (!sbuffer[i0]) continue; - if ((myobj != OVOID & obuffer[i0] != myobj)) + if ((myobj != OVOID) & (obuffer[i0] != myobj)) continue; tndx[nf][0] = (i-x)*(i-x) + (j-y)*(j-y); tndx[nf][1] = i0; @@ -249,7 +273,7 @@ int x, y; return(nf); } /* initialize offset array */ - if ((hres != hro | vres != vro)) { + if ((hres != hro) | (vres != vro)) { int toffs[NSCHECK][2]; i0 = fndx(SAMPDIST, SAMPDIST); nf = 0; @@ -268,9 +292,9 @@ int x, y; } /* find up to nc neighbors */ i0 = fndx(x, y); - for (j = 0, nf = 0; (j < NSCHECK & nf < nc); j++) { + for (j = 0, nf = 0; (j < NSCHECK) & (nf < nc); j++) { i = i0 + ioffs[j]; - if (sbuffer[i] && (myobj == OVOID | obuffer[i] == myobj)) + if (sbuffer[i] && (myobj == OVOID) | (obuffer[i] == myobj)) iarr[nf++] = i; } /* return number found */ @@ -280,13 +304,12 @@ int x, y; static void -setmotion(n, wpos) /* compute motion vector for this pixel */ -register int n; -FVECT wpos; +setmotion( /* compute motion vector for this pixel */ + register int n, + FVECT wpos +) { FVECT ovp; - MAT4 xfm; - double d; int moi; int xp, yp; /* ID object and update maximum HLS */ @@ -306,18 +329,18 @@ FVECT wpos; yp = (int)(ovp[1]*vres); xmbuffer[n] = xp - (n % hres); ymbuffer[n] = yp - (n / hres); - if ((xp < 0 | xp >= hres)) + if ((xp < 0) | (xp >= hres)) return; - if ((yp < 0 | yp >= vres)) + if ((yp < 0) | (yp >= vres)) return; n = fndx(xp, yp); - if ((zprev[n] < 0.97*ovp[2] | zprev[n] > 1.03*ovp[2])) + if ((zprev[n] < 0.97*ovp[2]) | (zprev[n] > 1.03*ovp[2])) oprev[n] = OVOID; /* assume it's a bad match */ } static void -init_frame_sample() /* sample our initial frame */ +init_frame_sample(void) /* sample our initial frame */ { RAY ir; int x, y; @@ -344,7 +367,7 @@ init_frame_sample() /* sample our initial frame */ continue; } if (!sample_here(x, y)) { /* just cast */ - rayorigin(&ir, NULL, PRIMARY, 1.0); + rayorigin(&ir, PRIMARY, NULL, NULL); if (!localhit(&ir, &thescene)) { if (ir.ro != &Aftplane) sourcehit(&ir); @@ -362,7 +385,7 @@ init_frame_sample() /* sample our initial frame */ } if (nprocs > 1) { /* get sample */ int rval; - rayorigin(&ir, NULL, PRIMARY, 1.0); + rayorigin(&ir, PRIMARY, NULL, NULL); ir.rno = n; rval = ray_pqueue(&ir); if (!rval) @@ -404,11 +427,11 @@ init_frame_sample() /* sample our initial frame */ n = fndx(x, y); if ((obj = obuffer[n]) == OVOID) continue; - if ((obuffer[n+1] != OVOID & obuffer[n+1] != obj)) { + if ((obuffer[n+1] != OVOID) & (obuffer[n+1] != obj)) { obuffer[n] = OVOID; obuffer[n+1] = OVOID; } - if ((obuffer[n+hres] != OVOID & obuffer[n+hres] != obj)) { + if ((obuffer[n+hres] != OVOID) & (obuffer[n+hres] != obj)) { obuffer[n] = OVOID; obuffer[n+hres] = OVOID; } @@ -419,17 +442,18 @@ init_frame_sample() /* sample our initial frame */ } -int -getambcolor(clr, obj) /* get ambient color for object if we can */ -COLOR clr; -int obj; +extern int +getambcolor( /* get ambient color for object if we can */ + COLOR clr, + int obj +) { register OBJREC *op; if (obj == OVOID) return(0); op = objptr(obj); - if ((op->otype == OBJ_INSTANCE & op->omod == OVOID)) + if ((op->otype == OBJ_INSTANCE) & (op->omod == OVOID)) return(0); /* search for material */ do { @@ -489,10 +513,13 @@ int obj; } -double -estimaterr(cs, cs2, ns, ns0) /* estimate relative error from samples */ -COLOR cs, cs2; -int ns; +extern double +estimaterr( /* estimate relative error from samples */ + COLOR cs, + COLOR cs2, + int ns, + int ns0 +) { double d, d2, brt; @@ -512,11 +539,12 @@ int ns; } -double -comperr(neigh, nc, ns0) /* estimate relative error in neighborhood */ -int *neigh; -int nc; -int ns0; +extern double +comperr( /* estimate relative error in neighborhood */ + int *neigh, + int nc, + int ns0 +) { COLOR csum, csum2; COLOR ctmp; @@ -526,7 +554,7 @@ int ns0; /* add together samples */ setcolor(csum, 0., 0., 0.); setcolor(csum2, 0., 0., 0.); - for (i = 0, ns = 0; (i < nc & ns < NSAMPOK); i++) { + for (i = 0, ns = 0; (i < nc) & (ns < NSAMPOK); i++) { n = neigh[i]; addcolor(csum, cbuffer[n]); if (val2map != NULL) { @@ -547,15 +575,15 @@ int ns0; } -void -comp_frame_error() /* initialize frame error values */ +extern void +comp_frame_error(void) /* initialize frame error values */ { BYTE *edone = NULL; COLOR objamb; double eest; int neigh[NSAMPOK]; int nc; - int x, y, i, j; + int x, y, i; register int n; if (!silent) { @@ -578,7 +606,7 @@ comp_frame_error() /* initialize frame error values * for (y = vres; y--; ) for (x = hres; x--; ) { n = fndx(x, y); - if ((abuffer[n] != ALOWQ | obuffer[n] == OVOID)) + if ((abuffer[n] != ALOWQ) | (obuffer[n] == OVOID)) continue; if (!getambcolor(objamb, obuffer[n])) continue; @@ -626,8 +654,8 @@ comp_frame_error() /* initialize frame error values * } -void -init_frame() /* render base (low quality) frame */ +extern void +init_frame(void) /* render base (low quality) frame */ { int restart; @@ -637,11 +665,12 @@ init_frame() /* render base (low quality) frame */ restart = (!nobjects || vdef(MOVE)); if (!restart && curparams != &lorendparams && nprocs > 1) restart = -1; - if (restart > 0) + if (restart > 0) { if (nprocs > 1) ray_pdone(1); else ray_done(1); + } /* post low quality parameters */ if (curparams != &lorendparams) ray_restore(curparams = &lorendparams); @@ -688,8 +717,8 @@ return; } -void -filter_frame() /* interpolation, motion-blur, and exposure */ +extern void +filter_frame(void) /* interpolation, motion-blur, and exposure */ { double expval = expspec_val(getexp(fcur)); int x, y; @@ -700,16 +729,16 @@ filter_frame() /* interpolation, motion-blur, and ex register int n; #if 0 -/* XXX TEMPORARY!! */ -conspicuity(); -write_map(cerrmap, "outcmap.pic"); -{ - float *ebuf = (float *)malloc(sizeof(float)*hres*vres); - for (n = hres*vres; n--; ) - ebuf[n] = acctab[abuffer[n]]; - write_map(ebuf, "outerr.pic"); - free((void *)ebuf); -} + /* XXX TEMPORARY!! */ + conspicuity(); + write_map(cerrmap, "outcmap.pic"); + { + float *ebuf = (float *)malloc(sizeof(float)*hres*vres); + for (n = hres*vres; n--; ) + ebuf[n] = acctab[abuffer[n]]; + write_map(ebuf, "outerr.pic"); + free((void *)ebuf); + } #endif if (!silent) { @@ -733,6 +762,16 @@ write_map(cerrmap, "outcmap.pic"); continue; nc = getclosest(neigh, NPINTERP, x, y); setcolor(cbuffer[n], 0., 0., 0.); + if (nc <= 0) { /* no acceptable neighbors */ + if (y < vres-1) + nc = fndx(x, y+1); + else if (x < hres-1) + nc = fndx(x+1, y); + else + continue; + copycolor(cbuffer[n], cbuffer[nc]); + continue; + } wsum = 0.; while (nc-- > 0) { copycolor(cval, cbuffer[neigh[nc]]); @@ -742,22 +781,19 @@ write_map(cerrmap, "outcmap.pic"); addcolor(cbuffer[n], cval); wsum += w; } - if (wsum > FTINY) { - w = 1.0/wsum; - scalecolor(cbuffer[n], w); - } + w = 1.0/wsum; + scalecolor(cbuffer[n], w); } /* motion blur if requested */ if (mblur > .02) { - int len; int xs, ys, xl, yl; int rise, run; long rise2, run2; int n2; int cnt; /* sum in motion streaks */ - bzero(outbuffer, sizeof(COLOR)*hres*vres); - bzero(wbuffer, 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); @@ -786,8 +822,8 @@ write_map(cerrmap, "outcmap.pic"); scalecolor(cval, w); while (cnt) if (rise2 >= run2) { - if ((xl >= 0 & xl < hres & - yl >= 0 & yl < vres)) { + if ((xl >= 0) & (xl < hres) & + (yl >= 0) & (yl < vres)) { n2 = fndx(xl, yl); addcolor(outbuffer[n2], cval); @@ -807,8 +843,8 @@ write_map(cerrmap, "outcmap.pic"); scalecolor(cval, w); while (cnt) if (run2 >= rise2) { - if ((xl >= 0 & xl < hres & - yl >= 0 & yl < vres)) { + if ((xl >= 0) & (xl < hres) & + (yl >= 0) & (yl < vres)) { n2 = fndx(xl, yl); addcolor(outbuffer[n2], cval); @@ -835,31 +871,32 @@ write_map(cerrmap, "outcmap.pic"); } else for (n = hres*vres; n--; ) copycolor(outbuffer[n], cbuffer[n]); -/* -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--; ) + 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); -return; -{ - float *sbuf = (float *)malloc(sizeof(float)*hres*vres); - char fnm[256]; - sprintf(fnm, vval(BASENAME), fcur); - strcat(fnm, "_outsamp.pic"); - for (n = hres*vres; n--; ) - sbuf[n] = (float)sbuffer[n]; - write_map(sbuf, fnm); - free((void *)sbuf); +#if 0 + { + float *sbuf = (float *)malloc(sizeof(float)*hres*vres); + char fnm[256]; + sprintf(fnm, vval(BASENAME), fcur); + strcat(fnm, "_outsamp.pic"); + for (n = hres*vres; n--; ) + sbuf[n] = (float)sbuffer[n]; + write_map(sbuf, fnm); + free((void *)sbuf); + } +#endif } -} -void -send_frame() /* send frame to destination */ +extern void +send_frame(void) /* send frame to destination */ { char pfname[1024]; double d; @@ -873,9 +910,7 @@ send_frame() /* send frame to destination */ sprintf(errmsg, "cannot open output frame \"%s\"", pfname); error(SYSTEM, errmsg); } -#ifdef MSDOS - setmode(fileno(fp), O_BINARY); -#endif + SET_FILE_BINARY(fp); if (!silent) { printf("\tWriting to \"%s\"\n", pfname); fflush(stdout); @@ -888,10 +923,10 @@ send_frame() /* send frame to destination */ fputnow(fp); fputs(VIEWSTR, fp); fprintview(&vw, fp); fputc('\n', fp); d = expspec_val(getexp(fcur)); - if ((d < 0.99 | d > 1.01)) + if ((d < 0.99) | (d > 1.01)) fputexpos(d, fp); d = viewaspect(&vw) * hres / vres; - if ((d < 0.99 | d > 1.01)) + if ((d < 0.99) | (d > 1.01)) fputaspect(d, fp); fputformat(COLRFMT, fp); fputc('\n', fp); /* end header */ @@ -913,8 +948,8 @@ writerr: } -void -free_frame() /* free frame allocation */ +extern void +free_frame(void) /* free frame allocation */ { if (cbuffer == NULL) return;