10 |
|
|
11 |
|
#include "standard.h" |
12 |
|
#include "rhd_qtree.h" |
13 |
+ |
/* quantity of leaves to free at a time */ |
14 |
+ |
#ifndef LFREEPCT |
15 |
+ |
#define LFREEPCT 25 |
16 |
+ |
#endif |
17 |
+ |
/* maximum allowed angle difference (deg.) */ |
18 |
+ |
#ifndef MAXANG |
19 |
+ |
#define MAXANG 20 |
20 |
+ |
#endif |
21 |
+ |
#if MAXANG>0 |
22 |
+ |
#define MAXDIFF2 ( MAXANG*MAXANG * (PI*PI/180./180.)) |
23 |
+ |
#endif |
24 |
|
|
25 |
+ |
#define abs(i) ((i) < 0 ? -(i) : (i)) |
26 |
+ |
|
27 |
|
RTREE qtrunk; /* our quadtree trunk */ |
28 |
< |
double qtDepthEps = .02; /* epsilon to compare depths (z fraction) */ |
28 |
> |
double qtDepthEps = .05; /* epsilon to compare depths (z fraction) */ |
29 |
|
int qtMinNodesiz = 2; /* minimum node dimension (pixels) */ |
30 |
+ |
struct rleaves qtL; /* our pile of leaves */ |
31 |
|
|
18 |
– |
static RLEAF *leafpile; /* our collection of leaf values */ |
19 |
– |
static int nleaves; /* count of leaves in our pile */ |
20 |
– |
static int bleaf, tleaf; /* bottom and top (next) leaf index (ring) */ |
21 |
– |
|
32 |
|
#define TBUNDLESIZ 409 /* number of twigs in a bundle */ |
33 |
|
|
34 |
|
static RTREE **twigbundle; /* free twig blocks (NULL term.) */ |
35 |
|
static int nexttwig; /* next free twig */ |
36 |
|
|
37 |
< |
static RTREE emptytree; /* empty tree for test below */ |
37 |
> |
#define ungetleaf(li) (qtL.tl=(li)) /* dangerous if used improperly */ |
38 |
|
|
29 |
– |
#define is_stump(t) (!bcmp((char *)(t), (char *)&emptytree, sizeof(RTREE))) |
39 |
|
|
31 |
– |
|
40 |
|
static RTREE * |
41 |
|
newtwig() /* allocate a twig */ |
42 |
|
{ |
71 |
|
{ |
72 |
|
register int i; |
73 |
|
|
74 |
< |
if (tmTop != NULL) |
67 |
< |
tmClearHisto(); |
68 |
< |
bzero((char *)&qtrunk, sizeof(RTREE)); |
69 |
< |
nexttwig = 0; |
74 |
> |
qtrunk.flgs = CH_ANY; /* chop down tree */ |
75 |
|
if (twigbundle == NULL) |
76 |
|
return; |
77 |
+ |
i = (TBUNDLESIZ-1+nexttwig)/TBUNDLESIZ; |
78 |
+ |
nexttwig = 0; |
79 |
|
if (!really) { /* just clear allocated blocks */ |
80 |
< |
for (i = 0; twigbundle[i] != NULL; i++) |
80 |
> |
while (i--) |
81 |
|
bzero((char *)twigbundle[i], TBUNDLESIZ*sizeof(RTREE)); |
82 |
|
return; |
83 |
|
} |
89 |
|
} |
90 |
|
|
91 |
|
|
92 |
< |
static RLEAF * |
92 |
> |
static int |
93 |
|
newleaf() /* allocate a leaf from our pile */ |
94 |
|
{ |
95 |
< |
if (tleaf++ >= nleaves) /* get next leaf in ring */ |
96 |
< |
tleaf = 0; |
97 |
< |
if (tleaf == bleaf) /* need to shake some free */ |
95 |
> |
int li; |
96 |
> |
|
97 |
> |
li = qtL.tl++; |
98 |
> |
if (qtL.tl >= qtL.nl) /* get next leaf in ring */ |
99 |
> |
qtL.tl = 0; |
100 |
> |
if (qtL.tl == qtL.bl) /* need to shake some free */ |
101 |
|
qtCompost(LFREEPCT); |
102 |
< |
return(leafpile + tleaf); |
102 |
> |
return(li); |
103 |
|
} |
104 |
|
|
105 |
|
|
106 |
+ |
#define LEAFSIZ (3*sizeof(float)+sizeof(int4)+\ |
107 |
+ |
sizeof(TMbright)+6*sizeof(BYTE)) |
108 |
+ |
|
109 |
|
int |
110 |
|
qtAllocLeaves(n) /* allocate space for n leaves */ |
111 |
< |
int n; |
111 |
> |
register int n; |
112 |
|
{ |
113 |
|
unsigned nbytes; |
114 |
|
register unsigned i; |
116 |
|
qtFreeTree(0); /* make sure tree is empty */ |
117 |
|
if (n <= 0) |
118 |
|
return(0); |
119 |
< |
if (nleaves >= n) |
120 |
< |
return(nleaves); |
121 |
< |
else if (nleaves > 0) |
122 |
< |
free((char *)leafpile); |
119 |
> |
if (qtL.nl >= n) |
120 |
> |
return(qtL.nl); |
121 |
> |
else if (qtL.nl > 0) |
122 |
> |
free(qtL.base); |
123 |
|
/* round space up to nearest power of 2 */ |
124 |
< |
nbytes = n*sizeof(RLEAF) + 8; |
124 |
> |
nbytes = n*LEAFSIZ + 8; |
125 |
|
for (i = 1024; nbytes > i; i <<= 1) |
126 |
|
; |
127 |
< |
n = (i - 8) / sizeof(RLEAF); |
128 |
< |
leafpile = (RLEAF *)malloc(n*sizeof(RLEAF)); |
129 |
< |
if (leafpile == NULL) |
130 |
< |
return(-1); |
131 |
< |
nleaves = n; |
132 |
< |
bleaf = tleaf = 0; |
133 |
< |
return(nleaves); |
127 |
> |
n = (i - 8) / LEAFSIZ; /* should we make sure n is even? */ |
128 |
> |
qtL.base = (char *)malloc(n*LEAFSIZ); |
129 |
> |
if (qtL.base == NULL) |
130 |
> |
return(0); |
131 |
> |
/* assign larger alignment types earlier */ |
132 |
> |
qtL.wp = (float (*)[3])qtL.base; |
133 |
> |
qtL.wd = (int4 *)(qtL.wp + n); |
134 |
> |
qtL.brt = (TMbright *)(qtL.wd + n); |
135 |
> |
qtL.chr = (BYTE (*)[3])(qtL.brt + n); |
136 |
> |
qtL.rgb = (BYTE (*)[3])(qtL.chr + n); |
137 |
> |
qtL.nl = n; |
138 |
> |
qtL.tml = qtL.bl = qtL.tl = 0; |
139 |
> |
return(n); |
140 |
|
} |
141 |
|
|
142 |
+ |
#undef LEAFSIZ |
143 |
|
|
144 |
+ |
|
145 |
|
qtFreeLeaves() /* free our allocated leaves and twigs */ |
146 |
|
{ |
147 |
|
qtFreeTree(1); /* free tree also */ |
148 |
< |
if (nleaves <= 0) |
148 |
> |
if (qtL.nl <= 0) |
149 |
|
return; |
150 |
< |
free((char *)leafpile); |
151 |
< |
leafpile = NULL; |
152 |
< |
nleaves = 0; |
150 |
> |
free(qtL.base); |
151 |
> |
qtL.base = NULL; |
152 |
> |
qtL.nl = 0; |
153 |
|
} |
154 |
|
|
155 |
|
|
160 |
|
register int i, li; |
161 |
|
|
162 |
|
for (i = 0; i < 4; i++) |
163 |
< |
if (tp->flgs & BRF(i)) |
163 |
> |
if (tp->flgs & BRF(i)) { |
164 |
|
shaketree(tp->k[i].b); |
165 |
< |
else if (tp->k[i].l != NULL) { |
166 |
< |
li = tp->k[i].l - leafpile; |
167 |
< |
if (bleaf < tleaf ? (li < bleaf || li >= tleaf) : |
168 |
< |
(li < bleaf && li >= tleaf)) { |
169 |
< |
tmAddHisto(&tp->k[i].l->brt, 1, -1); |
170 |
< |
tp->k[i].l = NULL; |
171 |
< |
} |
165 |
> |
if (is_stump(tp->k[i].b)) |
166 |
> |
tp->flgs &= ~BRF(i); |
167 |
> |
} else if (tp->flgs & LFF(i)) { |
168 |
> |
li = tp->k[i].li; |
169 |
> |
if (qtL.bl < qtL.tl ? |
170 |
> |
(li < qtL.bl || li >= qtL.tl) : |
171 |
> |
(li < qtL.bl && li >= qtL.tl)) |
172 |
> |
tp->flgs &= ~LFF(i); |
173 |
|
} |
174 |
|
} |
175 |
|
|
178 |
|
qtCompost(pct) /* free up some leaves */ |
179 |
|
int pct; |
180 |
|
{ |
181 |
< |
int nused, nclear; |
181 |
> |
int nused, nclear, nmapped; |
182 |
> |
|
183 |
|
/* figure out how many leaves to clear */ |
184 |
< |
nclear = nleaves * pct / 100; |
184 |
> |
nclear = qtL.nl * pct / 100; |
185 |
> |
nused = qtL.tl - qtL.bl; |
186 |
> |
if (nused <= 0) nused += qtL.nl; |
187 |
> |
nclear -= qtL.nl - nused; |
188 |
|
if (nclear <= 0) |
189 |
|
return(0); |
164 |
– |
nused = tleaf > bleaf ? tleaf-bleaf : tleaf+nleaves-bleaf; |
190 |
|
if (nclear >= nused) { /* clear them all */ |
191 |
|
qtFreeTree(0); |
192 |
< |
bleaf = tleaf = 0; |
192 |
> |
qtL.tml = qtL.bl = qtL.tl = 0; |
193 |
|
return(nused); |
194 |
|
} |
195 |
|
/* else clear leaves from bottom */ |
196 |
< |
bleaf = (bleaf + nclear) % nleaves; |
196 |
> |
nmapped = qtL.tml - qtL.bl; |
197 |
> |
if (nmapped < 0) nmapped += qtL.nl; |
198 |
> |
qtL.bl += nclear; |
199 |
> |
if (qtL.bl >= qtL.nl) qtL.bl -= qtL.nl; |
200 |
> |
if (nmapped <= nclear) qtL.tml = qtL.bl; |
201 |
|
shaketree(&qtrunk); |
202 |
|
return(nclear); |
203 |
|
} |
204 |
|
|
205 |
|
|
206 |
< |
RLEAF * |
206 |
> |
#define DCSCALE 11585.2 /* (1<<13)*sqrt(2) */ |
207 |
> |
#define FXNEG 01 |
208 |
> |
#define FYNEG 02 |
209 |
> |
#define FZNEG 04 |
210 |
> |
#define F1X 010 |
211 |
> |
#define F2Z 020 |
212 |
> |
#define F1SFT 5 |
213 |
> |
#define F2SFT 18 |
214 |
> |
#define FMASK 0x1fff |
215 |
> |
|
216 |
> |
static int4 |
217 |
> |
encodedir(dv) /* encode a normalized direction vector */ |
218 |
> |
FVECT dv; |
219 |
> |
{ |
220 |
> |
register int4 dc = 0; |
221 |
> |
int cd[3], cm; |
222 |
> |
register int i; |
223 |
> |
|
224 |
> |
for (i = 0; i < 3; i++) |
225 |
> |
if (dv[i] < 0.) { |
226 |
> |
cd[i] = dv[i] * -DCSCALE; |
227 |
> |
dc |= 1<<i; |
228 |
> |
} else |
229 |
> |
cd[i] = dv[i] * DCSCALE; |
230 |
> |
if (cd[0] <= cd[1]) { |
231 |
> |
dc |= F1X | cd[0] << F1SFT; |
232 |
> |
cm = cd[1]; |
233 |
> |
} else { |
234 |
> |
dc |= cd[1] << F1SFT; |
235 |
> |
cm = cd[0]; |
236 |
> |
} |
237 |
> |
if (cd[2] <= cm) |
238 |
> |
dc |= F2Z | cd[2] << F2SFT; |
239 |
> |
else |
240 |
> |
dc |= cm << F2SFT; |
241 |
> |
return(dc); |
242 |
> |
} |
243 |
> |
|
244 |
> |
|
245 |
> |
static |
246 |
> |
decodedir(dv, dc) /* decode a normalized direction vector */ |
247 |
> |
register FVECT dv; /* returned */ |
248 |
> |
register int4 dc; |
249 |
> |
{ |
250 |
> |
double d1, d2, der; |
251 |
> |
|
252 |
> |
d1 = ((dc>>F1SFT & FMASK)+.5)/DCSCALE; |
253 |
> |
d2 = ((dc>>F2SFT & FMASK)+.5)/DCSCALE; |
254 |
> |
der = sqrt(1. - d1*d1 - d2*d2); |
255 |
> |
if (dc & F1X) { |
256 |
> |
dv[0] = d1; |
257 |
> |
if (dc & F2Z) { dv[1] = der; dv[2] = d2; } |
258 |
> |
else { dv[1] = d2; dv[2] = der; } |
259 |
> |
} else { |
260 |
> |
dv[1] = d1; |
261 |
> |
if (dc & F2Z) { dv[0] = der; dv[2] = d2; } |
262 |
> |
else { dv[0] = d2; dv[2] = der; } |
263 |
> |
} |
264 |
> |
if (dc & FXNEG) dv[0] = -dv[0]; |
265 |
> |
if (dc & FYNEG) dv[1] = -dv[1]; |
266 |
> |
if (dc & FZNEG) dv[2] = -dv[2]; |
267 |
> |
} |
268 |
> |
|
269 |
> |
|
270 |
> |
static double |
271 |
> |
dir2diff(dc1, dc2) /* relative radians^2 between directions */ |
272 |
> |
int4 dc1, dc2; |
273 |
> |
{ |
274 |
> |
FVECT v1, v2; |
275 |
> |
|
276 |
> |
decodedir(v1, dc1); |
277 |
> |
decodedir(v2, dc2); |
278 |
> |
|
279 |
> |
return(2. - 2.*DOT(v1,v2)); |
280 |
> |
} |
281 |
> |
|
282 |
> |
|
283 |
> |
static double |
284 |
> |
fdir2diff(dc1, v2) /* relative radians^2 between directions */ |
285 |
> |
int4 dc1; |
286 |
> |
register FVECT v2; |
287 |
> |
{ |
288 |
> |
FVECT v1; |
289 |
> |
|
290 |
> |
decodedir(v1, dc1); |
291 |
> |
|
292 |
> |
return(2. - 2.*DOT(v1,v2)); |
293 |
> |
} |
294 |
> |
|
295 |
> |
|
296 |
> |
int |
297 |
|
qtFindLeaf(x, y) /* find closest leaf to (x,y) */ |
298 |
|
int x, y; |
299 |
|
{ |
300 |
|
register RTREE *tp = &qtrunk; |
301 |
< |
RLEAF *lp = NULL; |
301 |
> |
int li = -1; |
302 |
|
int x0=0, y0=0, x1=odev.hres, y1=odev.vres; |
303 |
|
int mx, my; |
304 |
|
register int q; |
305 |
|
/* check limits */ |
306 |
|
if (x < 0 || x >= odev.hres || y < 0 || y >= odev.vres) |
307 |
< |
return(NULL); |
307 |
> |
return(-1); |
308 |
|
/* find nearby leaf in our tree */ |
309 |
|
for ( ; ; ) { |
310 |
|
for (q = 0; q < 4; q++) /* find any leaf this level */ |
311 |
< |
if (!(tp->flgs & BRF(q)) && tp->k[q].l != NULL) { |
312 |
< |
lp = tp->k[q].l; |
311 |
> |
if (tp->flgs & LFF(q)) { |
312 |
> |
li = tp->k[q].li; |
313 |
|
break; |
314 |
|
} |
315 |
|
q = 0; /* which quadrant are we? */ |
323 |
|
tp = tp->k[q].b; |
324 |
|
continue; |
325 |
|
} |
326 |
< |
if (tp->k[q].l != NULL) /* good shot! */ |
327 |
< |
return(tp->k[q].l); |
328 |
< |
return(lp); /* else return what we have */ |
326 |
> |
if (tp->flgs & LFF(q)) /* good shot! */ |
327 |
> |
return(tp->k[q].li); |
328 |
> |
return(li); /* else return what we have */ |
329 |
|
} |
330 |
|
} |
331 |
|
|
332 |
|
|
333 |
|
static |
334 |
< |
addleaf(lp) /* add a leaf to our tree */ |
335 |
< |
RLEAF *lp; |
334 |
> |
addleaf(li) /* add a leaf to our tree */ |
335 |
> |
int li; |
336 |
|
{ |
337 |
|
register RTREE *tp = &qtrunk; |
338 |
|
int x0=0, y0=0, x1=odev.hres, y1=odev.vres; |
339 |
< |
RLEAF *lo = NULL; |
339 |
> |
int lo = -1; |
340 |
> |
double d2; |
341 |
|
int x, y, mx, my; |
342 |
|
double z; |
343 |
< |
FVECT ip, wp; |
343 |
> |
FVECT ip, wp, vd; |
344 |
|
register int q; |
345 |
< |
/* compute leaf location */ |
346 |
< |
VCOPY(wp, lp->wp); |
345 |
> |
/* compute leaf location in view */ |
346 |
> |
VCOPY(wp, qtL.wp[li]); |
347 |
|
viewloc(ip, &odev.v, wp); |
348 |
|
if (ip[2] <= 0. || ip[0] < 0. || ip[0] >= 1. |
349 |
|
|| ip[1] < 0. || ip[1] >= 1.) |
350 |
< |
return; |
350 |
> |
return(-1); /* behind or outside view */ |
351 |
> |
#ifdef DEBUG |
352 |
> |
if (odev.v.type == VT_PAR | odev.v.vfore > FTINY) |
353 |
> |
error(INTERNAL, "bad view assumption in addleaf"); |
354 |
> |
#endif |
355 |
> |
for (q = 0; q < 3; q++) |
356 |
> |
vd[q] = (wp[q] - odev.v.vp[q])/ip[2]; |
357 |
> |
d2 = fdir2diff(qtL.wd[li], vd); |
358 |
> |
#ifdef MAXDIFF2 |
359 |
> |
if (d2 > MAXDIFF2) |
360 |
> |
return(0); /* leaf dir. too far off */ |
361 |
> |
#endif |
362 |
|
x = ip[0] * odev.hres; |
363 |
|
y = ip[1] * odev.vres; |
364 |
|
z = ip[2]; |
376 |
|
tp = tp->k[q].b; |
377 |
|
continue; |
378 |
|
} |
379 |
< |
if (tp->k[q].l == NULL) { /* found stem for leaf */ |
380 |
< |
tp->k[q].l = lp; |
381 |
< |
tp->flgs |= CHF(q); |
379 |
> |
if (!(tp->flgs & LFF(q))) { /* found stem for leaf */ |
380 |
> |
tp->k[q].li = li; |
381 |
> |
tp->flgs |= CHLFF(q); |
382 |
|
break; |
383 |
|
} |
384 |
< |
/* check existing leaf */ |
385 |
< |
if (lo != tp->k[q].l) { |
386 |
< |
lo = tp->k[q].l; |
256 |
< |
VCOPY(wp, lo->wp); |
384 |
> |
if (lo != tp->k[q].li) { /* check old leaf */ |
385 |
> |
lo = tp->k[q].li; |
386 |
> |
VCOPY(wp, qtL.wp[lo]); |
387 |
|
viewloc(ip, &odev.v, wp); |
388 |
|
} |
389 |
|
/* is node minimum size? */ |
390 |
< |
if (x1-x0 <= qtMinNodesiz || y1-y0 <= qtMinNodesiz) { |
391 |
< |
if (z > (1.-qtDepthEps)*ip[2]) /* who is closer? */ |
392 |
< |
return; /* old one is */ |
393 |
< |
tp->k[q].l = lp; /* new one is */ |
390 |
> |
if (y1-y0 <= qtMinNodesiz || x1-x0 <= qtMinNodesiz) { |
391 |
> |
if (z > (1.+qtDepthEps)*ip[2]) |
392 |
> |
return(0); /* old one closer */ |
393 |
> |
if (z >= (1.-qtDepthEps)*ip[2] && |
394 |
> |
fdir2diff(qtL.wd[lo], vd) < d2) |
395 |
> |
return(0); /* old one better */ |
396 |
> |
tp->k[q].li = li; /* else new one is */ |
397 |
|
tp->flgs |= CHF(q); |
265 |
– |
tmAddHisto(&lo->brt, 1, -1); /* drop old one */ |
398 |
|
break; |
399 |
|
} |
400 |
< |
tp->flgs |= CHBRF(q); /* else grow tree */ |
400 |
> |
tp->flgs &= ~LFF(q); /* else grow tree */ |
401 |
> |
tp->flgs |= CHBRF(q); |
402 |
|
tp = tp->k[q].b = newtwig(); |
270 |
– |
tp->flgs |= CH_ANY; /* all new */ |
403 |
|
q = 0; /* old leaf -> new branch */ |
404 |
|
mx = ip[0] * odev.hres; |
405 |
|
my = ip[1] * odev.vres; |
406 |
|
if (mx >= (x0 + x1) >> 1) q |= 01; |
407 |
|
if (my >= (y0 + y1) >> 1) q |= 02; |
408 |
< |
tp->k[q].l = lo; |
408 |
> |
tp->flgs = CH_ANY|LFF(q); /* all new */ |
409 |
> |
tp->k[q].li = lo; |
410 |
|
} |
411 |
< |
tmAddHisto(&lp->brt, 1, 1); /* add leaf to histogram */ |
411 |
> |
return(1); /* done */ |
412 |
|
} |
413 |
|
|
414 |
|
|
415 |
< |
dev_value(c, p) /* add a pixel value to our output queue */ |
415 |
> |
dev_value(c, p, v) /* add a pixel value to our quadtree */ |
416 |
|
COLR c; |
417 |
< |
FVECT p; |
417 |
> |
FVECT p, v; |
418 |
|
{ |
419 |
< |
register RLEAF *lp; |
419 |
> |
register int li; |
420 |
|
|
421 |
< |
lp = newleaf(); |
422 |
< |
VCOPY(lp->wp, p); |
423 |
< |
tmCvColrs(&lp->brt, lp->chr, c, 1); |
424 |
< |
addleaf(lp); |
421 |
> |
li = newleaf(); |
422 |
> |
VCOPY(qtL.wp[li], p); |
423 |
> |
qtL.wd[li] = encodedir(v); |
424 |
> |
tmCvColrs(&qtL.brt[li], qtL.chr[li], c, 1); |
425 |
> |
if (!addleaf(li)) |
426 |
> |
ungetleaf(li); |
427 |
|
} |
428 |
|
|
429 |
|
|
430 |
|
qtReplant() /* replant our tree using new view */ |
431 |
|
{ |
432 |
|
register int i; |
433 |
< |
|
434 |
< |
if (bleaf == tleaf) /* anything to replant? */ |
433 |
> |
/* anything to replant? */ |
434 |
> |
if (qtL.bl == qtL.tl) |
435 |
|
return; |
436 |
< |
qtFreeTree(0); /* blow the tree away */ |
437 |
< |
/* now rebuild it */ |
438 |
< |
for (i = bleaf; i != tleaf; ) { |
439 |
< |
addleaf(leafpile+i); |
440 |
< |
if (++i >= nleaves) i = 0; |
436 |
> |
qtFreeTree(0); /* blow the old tree away */ |
437 |
> |
/* regrow it in new place */ |
438 |
> |
for (i = qtL.bl; i != qtL.tl; ) { |
439 |
> |
addleaf(i); |
440 |
> |
if (++i >= qtL.nl) i = 0; |
441 |
|
} |
307 |
– |
tmComputeMapping(0., 0., 0.); /* update the display */ |
308 |
– |
qtUpdate(); |
442 |
|
} |
443 |
|
|
444 |
|
|
445 |
< |
static |
446 |
< |
redraw(ca, tp, x0, y0, x1, y1, l) /* redraw portion of a tree */ |
314 |
< |
BYTE ca[3]; /* returned average color */ |
315 |
< |
register RTREE *tp; |
316 |
< |
int x0, y0, x1, y1; |
317 |
< |
int l[2][2]; |
445 |
> |
qtMapLeaves(redo) /* map our leaves to RGB */ |
446 |
> |
int redo; |
447 |
|
{ |
448 |
< |
int csm[3], nc; |
449 |
< |
BYTE rgb[3]; |
450 |
< |
int quads = CH_ANY; |
451 |
< |
int mx, my; |
452 |
< |
register int i; |
453 |
< |
/* compute midpoint */ |
454 |
< |
mx = (x0 + x1) >> 1; |
455 |
< |
my = (y0 + y1) >> 1; |
456 |
< |
/* see what to do */ |
457 |
< |
if (l[0][0] >= mx) |
458 |
< |
quads &= ~(CHF(2)|CHF(0)); |
459 |
< |
else if (l[0][1] <= mx) |
331 |
< |
quads &= ~(CHF(3)|CHF(1)); |
332 |
< |
if (l[1][0] >= my) |
333 |
< |
quads &= ~(CHF(1)|CHF(0)); |
334 |
< |
else if (l[1][1] <= my) |
335 |
< |
quads &= ~(CHF(3)|CHF(2)); |
336 |
< |
tp->flgs &= ~quads; /* mark them done */ |
337 |
< |
csm[0] = csm[1] = csm[2] = nc = 0; |
338 |
< |
/* do leaves first */ |
339 |
< |
for (i = 0; i < 4; i++) |
340 |
< |
if (quads & CHF(i) && !(tp->flgs & BRF(i)) && |
341 |
< |
tp->k[i].l != NULL) { |
342 |
< |
tmMapPixels(rgb, &tp->k[i].l->brt, tp->k[i].l->chr, 1); |
343 |
< |
dev_paintr(rgb, i&01 ? mx : x0, i&02 ? my : y0, |
344 |
< |
i&01 ? x1 : mx, i&02 ? y1 : my); |
345 |
< |
csm[0] += rgb[0]; csm[1] += rgb[1]; csm[2] += rgb[2]; |
346 |
< |
nc++; |
347 |
< |
quads &= ~CHF(i); |
348 |
< |
} |
349 |
< |
/* now do branches */ |
350 |
< |
for (i = 0; i < 4; i++) |
351 |
< |
if (quads & CHF(i) && tp->flgs & BRF(i)) { |
352 |
< |
redraw(rgb, tp->k[i].b, i&01 ? mx : x0, i&02 ? my : y0, |
353 |
< |
i&01 ? x1 : mx, i&02 ? y1 : my, l); |
354 |
< |
csm[0] += rgb[0]; csm[1] += rgb[1]; csm[2] += rgb[2]; |
355 |
< |
nc++; |
356 |
< |
quads &= ~CHF(i); |
357 |
< |
} |
358 |
< |
if (nc > 1) { |
359 |
< |
ca[0] = csm[0]/nc; ca[1] = csm[1]/nc; ca[2] = csm[2]/nc; |
448 |
> |
int aorg, alen, borg, blen; |
449 |
> |
/* recompute mapping? */ |
450 |
> |
if (redo) |
451 |
> |
qtL.tml = qtL.bl; |
452 |
> |
/* already done? */ |
453 |
> |
if (qtL.tml == qtL.tl) |
454 |
> |
return(1); |
455 |
> |
/* compute segments */ |
456 |
> |
aorg = qtL.tml; |
457 |
> |
if (qtL.tl >= aorg) { |
458 |
> |
alen = qtL.tl - aorg; |
459 |
> |
blen = 0; |
460 |
|
} else { |
461 |
< |
ca[0] = csm[0]; ca[1] = csm[1]; ca[2] = csm[2]; |
461 |
> |
alen = qtL.nl - aorg; |
462 |
> |
borg = 0; |
463 |
> |
blen = qtL.tl; |
464 |
|
} |
465 |
< |
if (!quads) return; |
466 |
< |
/* fill in gaps with average */ |
467 |
< |
for (i = 0; i < 4; i++) |
468 |
< |
if (quads & CHF(i)) |
469 |
< |
dev_paintr(ca, i&01 ? mx : x0, i&02 ? my : y0, |
470 |
< |
i&01 ? x1 : mx, i&02 ? y1 : my); |
471 |
< |
} |
472 |
< |
|
371 |
< |
|
372 |
< |
static |
373 |
< |
update(ca, tp, x0, y0, x1, y1) /* update tree display as needed */ |
374 |
< |
BYTE ca[3]; /* returned average color */ |
375 |
< |
register RTREE *tp; |
376 |
< |
int x0, y0, x1, y1; |
377 |
< |
{ |
378 |
< |
int csm[3], nc; |
379 |
< |
BYTE rgb[3]; |
380 |
< |
int gaps = 0; |
381 |
< |
int mx, my; |
382 |
< |
register int i; |
383 |
< |
/* compute midpoint */ |
384 |
< |
mx = (x0 + x1) >> 1; |
385 |
< |
my = (y0 + y1) >> 1; |
386 |
< |
csm[0] = csm[1] = csm[2] = nc = 0; |
387 |
< |
/* do leaves first */ |
388 |
< |
for (i = 0; i < 4; i++) |
389 |
< |
if ((tp->flgs & CHBRF(i)) == CHF(i)) { |
390 |
< |
if (tp->k[i].l == NULL) { |
391 |
< |
gaps |= 1<<i; /* empty stem */ |
392 |
< |
continue; |
393 |
< |
} |
394 |
< |
tmMapPixels(rgb, &tp->k[i].l->brt, tp->k[i].l->chr, 1); |
395 |
< |
dev_paintr(rgb, i&01 ? mx : x0, i&02 ? my : y0, |
396 |
< |
i&01 ? x1 : mx, i&02 ? y1 : my); |
397 |
< |
csm[0] += rgb[0]; csm[1] += rgb[1]; csm[2] += rgb[2]; |
398 |
< |
nc++; |
399 |
< |
} |
400 |
< |
/* now do branches */ |
401 |
< |
for (i = 0; i < 4; i++) |
402 |
< |
if ((tp->flgs & CHBRF(i)) == CHBRF(i)) { |
403 |
< |
update(rgb, tp->k[i].b, i&01 ? mx : x0, i&02 ? my : y0, |
404 |
< |
i&01 ? x1 : mx, i&02 ? y1 : my); |
405 |
< |
csm[0] += rgb[0]; csm[1] += rgb[1]; csm[2] += rgb[2]; |
406 |
< |
nc++; |
407 |
< |
} |
408 |
< |
if (nc > 1) { |
409 |
< |
ca[0] = csm[0]/nc; ca[1] = csm[1]/nc; ca[2] = csm[2]/nc; |
410 |
< |
} else { |
411 |
< |
ca[0] = csm[0]; ca[1] = csm[1]; ca[2] = csm[2]; |
465 |
> |
/* (re)compute tone mapping? */ |
466 |
> |
if (qtL.tml == qtL.bl) { |
467 |
> |
tmClearHisto(); |
468 |
> |
tmAddHisto(qtL.brt+aorg, alen, 1); |
469 |
> |
if (blen > 0) |
470 |
> |
tmAddHisto(qtL.brt+borg, blen, 1); |
471 |
> |
if (tmComputeMapping(0., 0., 0.) != TM_E_OK) |
472 |
> |
return(0); |
473 |
|
} |
474 |
< |
/* fill in gaps with average */ |
475 |
< |
for (i = 0; gaps && i < 4; gaps >>= 1, i++) |
476 |
< |
if (gaps & 01) |
477 |
< |
dev_paintr(ca, i&01 ? mx : x0, i&02 ? my : y0, |
478 |
< |
i&01 ? x1 : mx, i&02 ? y1 : my); |
479 |
< |
tp->flgs &= ~CH_ANY; /* all done */ |
480 |
< |
} |
481 |
< |
|
421 |
< |
|
422 |
< |
qtRedraw(x0, y0, x1, y1) /* redraw part of our screen */ |
423 |
< |
int x0, y0, x1, y1; |
424 |
< |
{ |
425 |
< |
int lim[2][2]; |
426 |
< |
BYTE ca[3]; |
427 |
< |
|
428 |
< |
if (is_stump(&qtrunk)) |
429 |
< |
return; |
430 |
< |
if ((lim[0][0]=x0) == 0 & (lim[1][0]=y0) == 0 & |
431 |
< |
(lim[0][1]=x1) == odev.hres & (lim[1][1]=y1) == odev.vres || |
432 |
< |
tmTop->lumap == NULL) |
433 |
< |
tmComputeMapping(0., 0., 0.); |
434 |
< |
redraw(ca, &qtrunk, 0, 0, odev.hres, odev.vres, lim); |
435 |
< |
} |
436 |
< |
|
437 |
< |
|
438 |
< |
qtUpdate() /* update our tree display */ |
439 |
< |
{ |
440 |
< |
BYTE ca[3]; |
441 |
< |
|
442 |
< |
if (is_stump(&qtrunk)) |
443 |
< |
return; |
444 |
< |
if (tmTop->lumap == NULL) |
445 |
< |
tmComputeMapping(0., 0., 0.); |
446 |
< |
update(ca, &qtrunk, 0, 0, odev.hres, odev.vres); |
474 |
> |
if (tmMapPixels(qtL.rgb+aorg, qtL.brt+aorg, |
475 |
> |
qtL.chr+aorg, alen) != TM_E_OK) |
476 |
> |
return(0); |
477 |
> |
if (blen > 0) |
478 |
> |
tmMapPixels(qtL.rgb+borg, qtL.brt+borg, |
479 |
> |
qtL.chr+borg, blen); |
480 |
> |
qtL.tml = qtL.tl; |
481 |
> |
return(1); |
482 |
|
} |