225 |
|
static float * |
226 |
|
getambdiffs(AMBHEMI *hp) |
227 |
|
{ |
228 |
< |
float *earr = (float *)calloc(hp->ns*hp->ns, sizeof(float)); |
228 |
> |
float *earr = (float *)malloc(sizeof(float)*hp->ns*hp->ns); |
229 |
|
float *ep; |
230 |
|
AMBSAMP *ap; |
231 |
|
double b, d2; |
236 |
|
/* compute squared neighbor diffs */ |
237 |
|
for (ap = hp->sa, ep = earr, i = 0; i < hp->ns; i++) |
238 |
|
for (j = 0; j < hp->ns; j++, ap++, ep++) { |
239 |
+ |
ep[0] = FTINY; |
240 |
|
b = bright(ap[0].v); |
241 |
|
if (i) { /* from above */ |
242 |
|
d2 = b - bright(ap[-hp->ns].v); |
273 |
|
ambsupersamp(double acol[3], AMBHEMI *hp, int cnt) |
274 |
|
{ |
275 |
|
float *earr = getambdiffs(hp); |
276 |
< |
double e2sum = 0.0; |
276 |
> |
double e2rem = 0; |
277 |
|
AMBSAMP *ap; |
278 |
|
RAY ar; |
279 |
|
double asum[3]; |
282 |
|
|
283 |
|
if (earr == NULL) /* just skip calc. if no memory */ |
284 |
|
return; |
285 |
< |
/* add up estimated variances */ |
285 |
> |
/* accumulate estimated variances */ |
286 |
|
for (ep = earr + hp->ns*hp->ns; ep-- > earr; ) |
287 |
< |
e2sum += *ep; |
287 |
> |
e2rem += *ep; |
288 |
|
ep = earr; /* perform super-sampling */ |
289 |
|
for (ap = hp->sa, i = 0; i < hp->ns; i++) |
290 |
|
for (j = 0; j < hp->ns; j++, ap++) { |
291 |
< |
int nss = *ep/e2sum*cnt + frandom(); |
291 |
> |
int nss = *ep/e2rem*cnt + frandom(); |
292 |
|
asum[0] = asum[1] = asum[2] = 0.0; |
293 |
|
for (n = 1; n <= nss; n++) { |
294 |
|
if (!getambsamp(&ar, hp, i, j, n)) { |
298 |
|
addcolor(asum, ar.rcol); |
299 |
|
} |
300 |
|
if (nss) { /* update returned ambient value */ |
301 |
< |
const double ssf = 1./(nss + 1); |
301 |
> |
const double ssf = 1./(nss + 1.); |
302 |
|
for (n = 3; n--; ) |
303 |
|
acol[n] += ssf*asum[n] + |
304 |
|
(ssf - 1.)*colval(ap->v,n); |
305 |
|
} |
306 |
< |
e2sum -= *ep++; /* update remainders */ |
306 |
> |
e2rem -= *ep++; /* update remainders */ |
307 |
|
cnt -= nss; |
308 |
|
} |
309 |
|
free(earr); |