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 */ |
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 */ |
48 |
|
double hlsmax; /* maximum high-level saliency this frame */ |
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); |
55 |
+ |
static void init_frame_sample(void); |
56 |
+ |
|
57 |
+ |
|
58 |
+ |
#if 0 |
59 |
|
void |
60 |
< |
write_map(mp, fn) /* write out float map (debugging) */ |
61 |
< |
float *mp; |
62 |
< |
char *fn; |
60 |
> |
write_map( /* write out float map (debugging) */ |
61 |
> |
float *mp, |
62 |
> |
char *fn |
63 |
> |
) |
64 |
|
{ |
65 |
|
FILE *fp = fopen(fn, "w"); |
66 |
|
COLOR scanbuf[2048]; |
81 |
|
} |
82 |
|
fclose(fp); |
83 |
|
} |
84 |
+ |
#endif |
85 |
|
|
86 |
|
|
87 |
|
static void |
88 |
< |
next_frame() /* prepare next frame buffer */ |
88 |
> |
next_frame(void) /* prepare next frame buffer */ |
89 |
|
{ |
90 |
|
VIEW *fv; |
91 |
|
char *err; |
92 |
|
/* get previous view */ |
93 |
|
if (vw.type != 0) |
94 |
< |
copystruct(&vwprev, &vw); |
94 |
> |
vwprev = vw; |
95 |
|
else if (fcur > 1 && (fv = getview(fcur-1)) != NULL) { |
96 |
< |
copystruct(&vwprev, fv); |
96 |
> |
vwprev = *fv; |
97 |
|
if (setview(&vwprev) != NULL) |
98 |
|
vwprev.type = 0; |
99 |
|
} |
102 |
|
sprintf(errmsg, "cannot get view for frame %d", fcur); |
103 |
|
error(USER, errmsg); |
104 |
|
} |
105 |
< |
copystruct(&vw, fv); |
105 |
> |
vw = *fv; |
106 |
|
if ((err = setview(&vw)) != NULL) { |
107 |
|
sprintf(errmsg, "view error at frame %d: %s", fcur, err); |
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: |
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: |
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 (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) | |
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 { |
134 |
< |
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 |
< |
bzero(abuffer, sizeof(BYTE)*hres*vres); |
171 |
< |
bzero(sbuffer, 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; |
181 |
|
|
182 |
|
|
183 |
|
static int |
184 |
< |
sample_here(x, y) /* 4x4 quincunx sample at this pixel? */ |
185 |
< |
register int x, y; |
184 |
> |
sample_here( /* 4x4 quincunx sample at this pixel? */ |
185 |
> |
int x, |
186 |
> |
int y |
187 |
> |
) |
188 |
|
{ |
189 |
|
if (y & 0x1) /* every other row has samples */ |
190 |
|
return(0); |
195 |
|
|
196 |
|
|
197 |
|
void |
198 |
< |
sample_pos(hv, x, y, sn) /* compute jittered sample position */ |
199 |
< |
double hv[2]; |
200 |
< |
int x, y; |
201 |
< |
int sn; |
198 |
> |
sample_pos( /* compute jittered sample position */ |
199 |
> |
double hv[2], |
200 |
> |
int x, |
201 |
> |
int y, |
202 |
> |
int sn |
203 |
> |
) |
204 |
|
{ |
205 |
|
int hl[2]; |
206 |
|
|
212 |
|
|
213 |
|
|
214 |
|
double |
215 |
< |
sample_wt(xo, yo) /* compute interpolant sample weight */ |
216 |
< |
int xo, yo; |
215 |
> |
sample_wt( /* compute interpolant sample weight */ |
216 |
> |
int xo, |
217 |
> |
int yo |
218 |
> |
) |
219 |
|
{ |
220 |
|
static double etab[400]; |
221 |
< |
int rad2 = xo*xo + yo*yo; |
221 |
> |
/* we can't use the name rad2 here, for some reason Visual C |
222 |
> |
thinks that is a constant (compiler bug?) */ |
223 |
> |
int rad_2 = xo*xo + yo*yo; |
224 |
|
int i; |
225 |
|
|
226 |
|
if (etab[0] <= FTINY) /* initialize exponent table */ |
228 |
|
etab[i] = exp(-0.1*i); |
229 |
|
|
230 |
|
/* look up Gaussian */ |
231 |
< |
i = (int)((10.*3./(double)SAMPDIST2)*rad2 + .5); |
231 |
> |
i = (int)((10.*3./(double)SAMPDIST2)*rad_2 + .5); |
232 |
|
if (i >= 400) |
233 |
|
return(0.0); |
234 |
|
return(etab[i]); |
236 |
|
|
237 |
|
|
238 |
|
static int |
239 |
< |
offset_cmp(p1, p2) /* compare offset distances */ |
240 |
< |
const void *p1, *p2; |
239 |
> |
offset_cmp( /* compare offset distances */ |
240 |
> |
const void *p1, |
241 |
> |
const void *p2 |
242 |
> |
) |
243 |
|
{ |
244 |
|
return(*(const int *)p1 - *(const int *)p2); |
245 |
|
} |
246 |
|
|
247 |
|
|
248 |
|
int |
249 |
< |
getclosest(iarr, nc, x, y) /* get nc closest neighbors on same object */ |
250 |
< |
int *iarr; |
251 |
< |
int nc; |
252 |
< |
int x, y; |
249 |
> |
getclosest( /* get nc closest neighbors on same object */ |
250 |
> |
int *iarr, |
251 |
> |
int nc, |
252 |
> |
int x, |
253 |
> |
int y |
254 |
> |
) |
255 |
|
{ |
256 |
|
#define NSCHECK ((2*SAMPDIST+1)*(2*SAMPDIST+1)) |
257 |
|
static int hro, vro; |
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 */ |
265 |
< |
if ((x < SAMPDIST | x >= hres-SAMPDIST | |
266 |
< |
y < SAMPDIST | y >= vres-SAMPDIST)) { |
265 |
> |
if ((x < SAMPDIST) | (x >= hres-SAMPDIST) | |
266 |
> |
(y < SAMPDIST) | (y >= vres-SAMPDIST)) { |
267 |
|
int tndx[NSCHECK][2]; |
268 |
|
nf = 0; |
269 |
|
for (j = y - SAMPDIST; j <= y + SAMPDIST; j++) { |
275 |
|
i0 = fndx(i, j); |
276 |
|
if (!sbuffer[i0]) |
277 |
|
continue; |
278 |
< |
if ((myobj != OVOID & obuffer[i0] != myobj)) |
278 |
> |
if ((myobj != OVOID) & (obuffer[i0] != myobj)) |
279 |
|
continue; |
280 |
|
tndx[nf][0] = (i-x)*(i-x) + (j-y)*(j-y); |
281 |
|
tndx[nf][1] = i0; |
290 |
|
return(nf); |
291 |
|
} |
292 |
|
/* initialize offset array */ |
293 |
< |
if ((hres != hro | vres != vro)) { |
293 |
> |
if ((hres != hro) | (vres != vro)) { |
294 |
|
int toffs[NSCHECK][2]; |
295 |
|
i0 = fndx(SAMPDIST, SAMPDIST); |
296 |
|
nf = 0; |
309 |
|
} |
310 |
|
/* find up to nc neighbors */ |
311 |
|
i0 = fndx(x, y); |
312 |
< |
for (j = 0, nf = 0; (j < NSCHECK & nf < nc); j++) { |
312 |
> |
for (j = 0, nf = 0; (j < NSCHECK) & (nf < nc); j++) { |
313 |
|
i = i0 + ioffs[j]; |
314 |
< |
if (sbuffer[i] && (myobj == OVOID | obuffer[i] == myobj)) |
314 |
> |
if (sbuffer[i] && (myobj == OVOID) | (obuffer[i] == myobj)) |
315 |
|
iarr[nf++] = i; |
316 |
|
} |
317 |
|
/* return number found */ |
321 |
|
|
322 |
|
|
323 |
|
static void |
324 |
< |
setmotion(n, wpos) /* compute motion vector for this pixel */ |
325 |
< |
register int n; |
326 |
< |
FVECT wpos; |
324 |
> |
setmotion( /* compute motion vector for this pixel */ |
325 |
> |
int n, |
326 |
> |
FVECT wpos |
327 |
> |
) |
328 |
|
{ |
329 |
|
FVECT ovp; |
289 |
– |
MAT4 xfm; |
290 |
– |
double d; |
330 |
|
int moi; |
331 |
|
int xp, yp; |
332 |
|
/* ID object and update maximum HLS */ |
346 |
|
yp = (int)(ovp[1]*vres); |
347 |
|
xmbuffer[n] = xp - (n % hres); |
348 |
|
ymbuffer[n] = yp - (n / hres); |
349 |
< |
if ((xp < 0 | xp >= hres)) |
349 |
> |
if ((xp < 0) | (xp >= hres)) |
350 |
|
return; |
351 |
< |
if ((yp < 0 | yp >= vres)) |
351 |
> |
if ((yp < 0) | (yp >= vres)) |
352 |
|
return; |
353 |
|
n = fndx(xp, yp); |
354 |
< |
if ((zprev[n] < 0.97*ovp[2] | zprev[n] > 1.03*ovp[2])) |
354 |
> |
if ((zprev[n] < 0.97*ovp[2]) | (zprev[n] > 1.03*ovp[2])) |
355 |
|
oprev[n] = OVOID; /* assume it's a bad match */ |
356 |
|
} |
357 |
|
|
358 |
|
|
359 |
|
static void |
360 |
< |
init_frame_sample() /* sample our initial frame */ |
360 |
> |
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..."); |
372 |
|
for (x = hres; x--; ) { |
373 |
|
double hv[2]; |
374 |
|
n = fndx(x, y); |
375 |
< |
xmbuffer[n] = MO_UNK; |
337 |
< |
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) { |
383 |
|
continue; |
384 |
|
} |
385 |
|
if (!sample_here(x, y)) { /* just cast */ |
386 |
< |
rayorigin(&ir, NULL, PRIMARY, 1.0); |
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; |
403 |
|
} |
404 |
|
if (nprocs > 1) { /* get sample */ |
405 |
|
int rval; |
406 |
< |
rayorigin(&ir, NULL, PRIMARY, 1.0); |
406 |
> |
rayorigin(&ir, PRIMARY, NULL, NULL); |
407 |
|
ir.rno = n; |
408 |
|
rval = ray_pqueue(&ir); |
409 |
|
if (!rval) |
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; |
445 |
|
n = fndx(x, y); |
446 |
|
if ((obj = obuffer[n]) == OVOID) |
447 |
|
continue; |
448 |
< |
if ((obuffer[n+1] != OVOID & obuffer[n+1] != obj)) { |
448 |
> |
if ((obuffer[n+1] != OVOID) & (obuffer[n+1] != obj)) { |
449 |
|
obuffer[n] = OVOID; |
450 |
|
obuffer[n+1] = OVOID; |
451 |
|
} |
452 |
< |
if ((obuffer[n+hres] != OVOID & obuffer[n+hres] != obj)) { |
452 |
> |
if ((obuffer[n+hres] != OVOID) & (obuffer[n+hres] != obj)) { |
453 |
|
obuffer[n] = OVOID; |
454 |
|
obuffer[n+hres] = OVOID; |
455 |
|
} |
461 |
|
|
462 |
|
|
463 |
|
int |
464 |
< |
getambcolor(clr, obj) /* get ambient color for object if we can */ |
465 |
< |
COLOR clr; |
466 |
< |
int obj; |
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) |
438 |
< |
return(0); |
439 |
< |
op = objptr(op->omod); |
440 |
< |
} 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, |
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) { |
529 |
|
|
530 |
|
|
531 |
|
double |
532 |
< |
estimaterr(cs, cs2, ns, ns0) /* estimate relative error from samples */ |
533 |
< |
COLOR cs, cs2; |
534 |
< |
int ns; |
532 |
> |
estimaterr( /* estimate relative error from samples */ |
533 |
> |
COLOR cs, |
534 |
> |
COLOR cs2, |
535 |
> |
int ns, |
536 |
> |
int ns0 |
537 |
> |
) |
538 |
|
{ |
539 |
|
double d, d2, brt; |
540 |
|
|
555 |
|
|
556 |
|
|
557 |
|
double |
558 |
< |
comperr(neigh, nc, ns0) /* estimate relative error in neighborhood */ |
559 |
< |
int *neigh; |
560 |
< |
int nc; |
561 |
< |
int ns0; |
558 |
> |
comperr( /* estimate relative error in neighborhood */ |
559 |
> |
int *neigh, |
560 |
> |
int nc, |
561 |
> |
int ns0 |
562 |
> |
) |
563 |
|
{ |
564 |
|
COLOR csum, csum2; |
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.); |
572 |
< |
for (i = 0, ns = 0; (i < nc & ns < NSAMPOK); i++) { |
572 |
> |
for (i = 0, ns = 0; (i < nc) & (ns < NSAMPOK); i++) { |
573 |
|
n = neigh[i]; |
574 |
|
addcolor(csum, cbuffer[n]); |
575 |
|
if (val2map != NULL) { |
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), |
542 |
< |
colval(cbuffer[n],GRN)*colval(cbuffer[n],GRN), |
543 |
< |
colval(cbuffer[n],BLU)*colval(cbuffer[n],BLU)); |
582 |
> |
copycolor(ctmp, cbuffer[n]); |
583 |
> |
multcolor(ctmp, ctmp); |
584 |
|
addcolor(csum2, ctmp); |
585 |
|
ns++; |
586 |
|
} |
589 |
|
|
590 |
|
|
591 |
|
void |
592 |
< |
comp_frame_error() /* initialize frame error values */ |
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, j; |
600 |
< |
register int n; |
599 |
> |
int x, y, i; |
600 |
> |
int n; |
601 |
|
|
602 |
|
if (!silent) { |
603 |
|
printf("\tComputing error map\n"); |
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--; ) |
620 |
< |
for (x = hres; x--; ) { |
581 |
< |
n = fndx(x, y); |
582 |
< |
if ((abuffer[n] != ALOWQ | obuffer[n] == OVOID)) |
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])) |
623 |
|
continue; |
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--; ) |
666 |
|
|
667 |
|
|
668 |
|
void |
669 |
< |
init_frame() /* render base (low quality) frame */ |
669 |
> |
init_frame(void) /* render base (low quality) frame */ |
670 |
|
{ |
671 |
|
int restart; |
634 |
– |
|
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; |
641 |
– |
if (restart > 0) |
642 |
– |
if (nprocs > 1) |
643 |
– |
ray_pdone(1); |
644 |
– |
else |
645 |
– |
ray_done(1); |
678 |
|
/* post low quality parameters */ |
679 |
|
if (curparams != &lorendparams) |
680 |
|
ray_restore(curparams = &lorendparams); |
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); |
718 |
|
write_map(ebuf, fnm); |
719 |
|
free((void *)ebuf); |
720 |
|
} |
721 |
+ |
#endif |
722 |
|
} |
723 |
|
|
724 |
|
|
725 |
|
void |
726 |
< |
filter_frame() /* interpolation, motion-blur, and exposure */ |
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!! */ |
738 |
< |
conspicuity(); |
739 |
< |
write_map(cerrmap, "outcmap.pic"); |
740 |
< |
{ |
741 |
< |
float *ebuf = (float *)malloc(sizeof(float)*hres*vres); |
742 |
< |
for (n = hres*vres; n--; ) |
743 |
< |
ebuf[n] = acctab[abuffer[n]]; |
744 |
< |
write_map(ebuf, "outerr.pic"); |
745 |
< |
free((void *)ebuf); |
746 |
< |
} |
737 |
> |
/* XXX TEMPORARY!! */ |
738 |
> |
conspicuity(); |
739 |
> |
write_map(cerrmap, "outcmap.pic"); |
740 |
> |
{ |
741 |
> |
float *ebuf = (float *)malloc(sizeof(float)*hres*vres); |
742 |
> |
for (n = hres*vres; n--; ) |
743 |
> |
ebuf[n] = acctab[abuffer[n]]; |
744 |
> |
write_map(ebuf, "outerr.pic"); |
745 |
> |
free((void *)ebuf); |
746 |
> |
} |
747 |
|
#endif |
715 |
– |
|
748 |
|
if (!silent) { |
749 |
|
printf("\tFiltering frame\n"); |
750 |
|
fflush(stdout); |
751 |
|
} |
752 |
|
/* normalize samples */ |
753 |
< |
for (y = vres; y--; ) |
722 |
< |
for (x = hres; x--; ) { |
723 |
< |
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--; ) { |
764 |
|
continue; |
765 |
|
nc = getclosest(neigh, NPINTERP, x, y); |
766 |
|
setcolor(cbuffer[n], 0., 0., 0.); |
767 |
+ |
if (nc <= 0) { /* no acceptable neighbors */ |
768 |
+ |
if (y < vres-1) |
769 |
+ |
nc = fndx(x, y+1); |
770 |
+ |
else if (x < hres-1) |
771 |
+ |
nc = fndx(x+1, y); |
772 |
+ |
else |
773 |
+ |
continue; |
774 |
+ |
copycolor(cbuffer[n], cbuffer[nc]); |
775 |
+ |
continue; |
776 |
+ |
} |
777 |
|
wsum = 0.; |
778 |
|
while (nc-- > 0) { |
779 |
|
copycolor(cval, cbuffer[neigh[nc]]); |
783 |
|
addcolor(cbuffer[n], cval); |
784 |
|
wsum += w; |
785 |
|
} |
786 |
< |
if (wsum > FTINY) { |
787 |
< |
w = 1.0/wsum; |
748 |
< |
scalecolor(cbuffer[n], w); |
749 |
< |
} |
786 |
> |
w = 1.0/wsum; |
787 |
> |
scalecolor(cbuffer[n], w); |
788 |
|
} |
789 |
|
/* motion blur if requested */ |
790 |
< |
if (mblur > .02) { |
753 |
< |
int len; |
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 |
< |
bzero(outbuffer, sizeof(COLOR)*hres*vres); |
800 |
< |
bzero(wbuffer, 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); |
812 |
|
wbuffer[n] += 1.; |
813 |
|
continue; |
814 |
|
} |
815 |
< |
xl = x - run/4; |
777 |
< |
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; } |
825 |
|
scalecolor(cval, w); |
826 |
|
while (cnt) |
827 |
|
if (rise2 >= run2) { |
828 |
< |
if ((xl >= 0 & xl < hres & |
829 |
< |
yl >= 0 & yl < vres)) { |
828 |
> |
if ((xl >= 0) & (xl < hres) & |
829 |
> |
(yl >= 0) & (yl < vres)) { |
830 |
|
n2 = fndx(xl, yl); |
831 |
|
addcolor(outbuffer[n2], |
832 |
|
cval); |
846 |
|
scalecolor(cval, w); |
847 |
|
while (cnt) |
848 |
|
if (run2 >= rise2) { |
849 |
< |
if ((xl >= 0 & xl < hres & |
850 |
< |
yl >= 0 & yl < vres)) { |
849 |
> |
if ((xl >= 0) & (xl < hres) & |
850 |
> |
(yl >= 0) & (yl < vres)) { |
851 |
|
n2 = fndx(xl, yl); |
852 |
|
addcolor(outbuffer[n2], |
853 |
|
cval); |
863 |
|
} |
864 |
|
} |
865 |
|
/* compute final results */ |
828 |
– |
for (y = vres; y--; ) |
829 |
– |
for (x = hres; x--; ) { |
830 |
– |
n = fndx(x, y); |
831 |
– |
if (wbuffer[n] <= FTINY) |
832 |
– |
continue; |
833 |
– |
w = 1./wbuffer[n]; |
834 |
– |
scalecolor(outbuffer[n], w); |
835 |
– |
} |
836 |
– |
} else |
866 |
|
for (n = hres*vres; n--; ) |
867 |
< |
copycolor(outbuffer[n], cbuffer[n]); |
868 |
< |
/* |
869 |
< |
for (n = hres*vres; n--; ) |
870 |
< |
if (!sbuffer[n]) |
871 |
< |
setcolor(outbuffer[n], 0., 0., 0.); |
872 |
< |
*/ |
873 |
< |
/* adjust exposure */ |
874 |
< |
if ((expval < 0.99 | expval > 1.01)) |
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 |
> |
*/ |
885 |
> |
#if 0 |
886 |
> |
{ |
887 |
> |
float *sbuf = (float *)malloc(sizeof(float)*hres*vres); |
888 |
> |
char fnm[256]; |
889 |
> |
sprintf(fnm, vval(BASENAME), fcur); |
890 |
> |
strcat(fnm, "_outsamp.pic"); |
891 |
|
for (n = hres*vres; n--; ) |
892 |
< |
scalecolor(outbuffer[n], expval); |
893 |
< |
return; |
894 |
< |
{ |
895 |
< |
float *sbuf = (float *)malloc(sizeof(float)*hres*vres); |
896 |
< |
char fnm[256]; |
852 |
< |
sprintf(fnm, vval(BASENAME), fcur); |
853 |
< |
strcat(fnm, "_outsamp.pic"); |
854 |
< |
for (n = hres*vres; n--; ) |
855 |
< |
sbuf[n] = (float)sbuffer[n]; |
856 |
< |
write_map(sbuf, fnm); |
857 |
< |
free((void *)sbuf); |
892 |
> |
sbuf[n] = (float)sbuffer[n]; |
893 |
> |
write_map(sbuf, fnm); |
894 |
> |
free((void *)sbuf); |
895 |
> |
} |
896 |
> |
#endif |
897 |
|
} |
859 |
– |
} |
898 |
|
|
899 |
|
|
900 |
|
void |
901 |
< |
send_frame() /* send frame to destination */ |
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 */ |
925 |
|
fputnow(fp); |
926 |
|
fputs(VIEWSTR, fp); fprintview(&vw, fp); fputc('\n', fp); |
927 |
|
d = expspec_val(getexp(fcur)); |
928 |
< |
if ((d < 0.99 | d > 1.01)) |
928 |
> |
if ((d < 0.99) | (d > 1.01)) |
929 |
|
fputexpos(d, fp); |
930 |
|
d = viewaspect(&vw) * hres / vres; |
931 |
< |
if ((d < 0.99 | d > 1.01)) |
931 |
> |
if ((d < 0.99) | (d > 1.01)) |
932 |
|
fputaspect(d, fp); |
933 |
|
fputformat(COLRFMT, fp); |
934 |
|
fputc('\n', fp); /* end header */ |
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 |
|
void |
1008 |
< |
free_frame() /* free frame allocation */ |
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; |