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 |
|
|
32 |
< |
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) */ |
32 |
> |
static int4 falleaves; /* our list of fallen leaves */ |
33 |
|
|
34 |
+ |
#define composted(li) (qtL.bl <= qtL.tl ? \ |
35 |
+ |
((li) < qtL.bl || (li) >= qtL.tl) : \ |
36 |
+ |
((li) < qtL.bl && (li) >= qtL.tl)) |
37 |
+ |
|
38 |
|
#define TBUNDLESIZ 409 /* number of twigs in a bundle */ |
39 |
|
|
40 |
|
static RTREE **twigbundle; /* free twig blocks (NULL term.) */ |
41 |
|
static int nexttwig; /* next free twig */ |
42 |
|
|
27 |
– |
static RTREE emptytree; /* empty tree for test below */ |
43 |
|
|
29 |
– |
#define is_stump(t) (!bcmp((char *)(t), (char *)&emptytree, sizeof(RTREE))) |
30 |
– |
|
31 |
– |
|
44 |
|
static RTREE * |
45 |
|
newtwig() /* allocate a twig */ |
46 |
|
{ |
75 |
|
{ |
76 |
|
register int i; |
77 |
|
|
78 |
< |
tmClearHisto(); |
67 |
< |
bzero((char *)&qtrunk, sizeof(RTREE)); |
68 |
< |
nexttwig = 0; |
78 |
> |
qtrunk.flgs = CH_ANY; /* chop down tree */ |
79 |
|
if (twigbundle == NULL) |
80 |
|
return; |
81 |
+ |
i = (TBUNDLESIZ-1+nexttwig)/TBUNDLESIZ; |
82 |
+ |
nexttwig = 0; |
83 |
|
if (!really) { /* just clear allocated blocks */ |
84 |
< |
for (i = 0; twigbundle[i] != NULL; i++) |
84 |
> |
while (i--) |
85 |
|
bzero((char *)twigbundle[i], TBUNDLESIZ*sizeof(RTREE)); |
86 |
|
return; |
87 |
|
} |
93 |
|
} |
94 |
|
|
95 |
|
|
96 |
< |
static RLEAF * |
97 |
< |
newleaf() /* allocate a leaf from our pile */ |
86 |
< |
{ |
87 |
< |
RLEAF *lp; |
88 |
< |
|
89 |
< |
lp = leafpile + tleaf++; |
90 |
< |
if (tleaf >= nleaves) /* get next leaf in ring */ |
91 |
< |
tleaf = 0; |
92 |
< |
if (tleaf == bleaf) /* need to shake some free */ |
93 |
< |
qtCompost(LFREEPCT); |
94 |
< |
return(lp); |
95 |
< |
} |
96 |
> |
#define LEAFSIZ (3*sizeof(float)+sizeof(int4)+\ |
97 |
> |
sizeof(TMbright)+6*sizeof(BYTE)) |
98 |
|
|
97 |
– |
|
99 |
|
int |
100 |
|
qtAllocLeaves(n) /* allocate space for n leaves */ |
101 |
< |
int n; |
101 |
> |
register int n; |
102 |
|
{ |
103 |
|
unsigned nbytes; |
104 |
|
register unsigned i; |
106 |
|
qtFreeTree(0); /* make sure tree is empty */ |
107 |
|
if (n <= 0) |
108 |
|
return(0); |
109 |
< |
if (nleaves >= n) |
110 |
< |
return(nleaves); |
111 |
< |
else if (nleaves > 0) |
112 |
< |
free((char *)leafpile); |
109 |
> |
if (qtL.nl >= n) |
110 |
> |
return(qtL.nl); |
111 |
> |
else if (qtL.nl > 0) |
112 |
> |
free(qtL.base); |
113 |
|
/* round space up to nearest power of 2 */ |
114 |
< |
nbytes = n*sizeof(RLEAF) + 8; |
114 |
> |
nbytes = n*LEAFSIZ + 8; |
115 |
|
for (i = 1024; nbytes > i; i <<= 1) |
116 |
|
; |
117 |
< |
n = (i - 8) / sizeof(RLEAF); |
118 |
< |
leafpile = (RLEAF *)malloc(n*sizeof(RLEAF)); |
119 |
< |
if (leafpile == NULL) |
120 |
< |
return(-1); |
121 |
< |
nleaves = n; |
122 |
< |
bleaf = tleaf = 0; |
123 |
< |
return(nleaves); |
117 |
> |
n = (i - 8) / LEAFSIZ; /* should we make sure n is even? */ |
118 |
> |
qtL.base = (char *)malloc(n*LEAFSIZ); |
119 |
> |
if (qtL.base == NULL) |
120 |
> |
return(0); |
121 |
> |
/* assign larger alignment types earlier */ |
122 |
> |
qtL.wp = (float (*)[3])qtL.base; |
123 |
> |
qtL.wd = (int4 *)(qtL.wp + n); |
124 |
> |
qtL.brt = (TMbright *)(qtL.wd + n); |
125 |
> |
qtL.chr = (BYTE (*)[3])(qtL.brt + n); |
126 |
> |
qtL.rgb = (BYTE (*)[3])(qtL.chr + n); |
127 |
> |
qtL.nl = n; |
128 |
> |
qtL.tml = qtL.bl = qtL.tl = 0; |
129 |
> |
falleaves = -1; |
130 |
> |
return(n); |
131 |
|
} |
132 |
|
|
133 |
+ |
#undef LEAFSIZ |
134 |
|
|
135 |
+ |
|
136 |
|
qtFreeLeaves() /* free our allocated leaves and twigs */ |
137 |
|
{ |
138 |
|
qtFreeTree(1); /* free tree also */ |
139 |
< |
if (nleaves <= 0) |
139 |
> |
if (qtL.nl <= 0) |
140 |
|
return; |
141 |
< |
free((char *)leafpile); |
142 |
< |
leafpile = NULL; |
143 |
< |
nleaves = 0; |
141 |
> |
free(qtL.base); |
142 |
> |
qtL.base = NULL; |
143 |
> |
qtL.nl = 0; |
144 |
|
} |
145 |
|
|
146 |
|
|
151 |
|
register int i, li; |
152 |
|
|
153 |
|
for (i = 0; i < 4; i++) |
154 |
< |
if (tp->flgs & BRF(i)) |
154 |
> |
if (tp->flgs & BRF(i)) { |
155 |
|
shaketree(tp->k[i].b); |
156 |
< |
else if (tp->k[i].l != NULL) { |
157 |
< |
li = tp->k[i].l - leafpile; |
158 |
< |
if (bleaf < tleaf ? (li < bleaf || li >= tleaf) : |
159 |
< |
(li < bleaf && li >= tleaf)) { |
160 |
< |
tmAddHisto(&tp->k[i].l->brt, 1, -1); |
161 |
< |
tp->k[i].l = NULL; |
152 |
< |
} |
156 |
> |
if (is_stump(tp->k[i].b)) |
157 |
> |
tp->flgs &= ~BRF(i); |
158 |
> |
} else if (tp->flgs & LFF(i)) { |
159 |
> |
li = tp->k[i].li; |
160 |
> |
if (composted(li)) |
161 |
> |
tp->flgs &= ~LFF(i); |
162 |
|
} |
163 |
|
} |
164 |
|
|
167 |
|
qtCompost(pct) /* free up some leaves */ |
168 |
|
int pct; |
169 |
|
{ |
170 |
< |
int nused, nclear; |
171 |
< |
|
163 |
< |
if (is_stump(&qtrunk)) |
164 |
< |
return(0); |
170 |
> |
register int4 *fl; |
171 |
> |
int nused, nclear, nmapped; |
172 |
|
/* figure out how many leaves to clear */ |
173 |
< |
nclear = nleaves * pct / 100; |
174 |
< |
nused = tleaf > bleaf ? tleaf-bleaf : tleaf+nleaves-bleaf; |
175 |
< |
nclear -= nleaves - nused; /* less what's already free */ |
173 |
> |
nclear = qtL.nl * pct / 100; |
174 |
> |
nused = qtL.tl - qtL.bl; |
175 |
> |
if (nused <= 0) nused += qtL.nl; |
176 |
> |
nclear -= qtL.nl - nused; |
177 |
|
if (nclear <= 0) |
178 |
|
return(0); |
179 |
|
if (nclear >= nused) { /* clear them all */ |
180 |
|
qtFreeTree(0); |
181 |
< |
bleaf = tleaf = 0; |
181 |
> |
qtL.tml = qtL.bl = qtL.tl = 0; |
182 |
> |
falleaves = -1; |
183 |
|
return(nused); |
184 |
|
} |
185 |
|
/* else clear leaves from bottom */ |
186 |
< |
bleaf += nclear; |
187 |
< |
if (bleaf >= nleaves) bleaf -= nleaves; |
188 |
< |
shaketree(&qtrunk); |
186 |
> |
nmapped = qtL.tml - qtL.bl; |
187 |
> |
if (nmapped < 0) nmapped += qtL.nl; |
188 |
> |
qtL.bl += nclear; |
189 |
> |
if (qtL.bl >= qtL.nl) qtL.bl -= qtL.nl; |
190 |
> |
if (nmapped <= nclear) qtL.tml = qtL.bl; |
191 |
> |
shaketree(&qtrunk); /* dereference composted leaves */ |
192 |
> |
for (fl = &falleaves; *fl >= 0; fl = qtL.wd + *fl) |
193 |
> |
while (composted(*fl)) |
194 |
> |
if ((*fl = qtL.wd[*fl]) < 0) |
195 |
> |
return(nclear); |
196 |
|
return(nclear); |
197 |
|
} |
198 |
|
|
199 |
|
|
200 |
< |
RLEAF * |
200 |
> |
int |
201 |
|
qtFindLeaf(x, y) /* find closest leaf to (x,y) */ |
202 |
|
int x, y; |
203 |
|
{ |
204 |
|
register RTREE *tp = &qtrunk; |
205 |
< |
RLEAF *lp = NULL; |
205 |
> |
int li = -1; |
206 |
|
int x0=0, y0=0, x1=odev.hres, y1=odev.vres; |
207 |
|
int mx, my; |
208 |
|
register int q; |
209 |
|
/* check limits */ |
210 |
|
if (x < 0 || x >= odev.hres || y < 0 || y >= odev.vres) |
211 |
< |
return(NULL); |
211 |
> |
return(-1); |
212 |
|
/* find nearby leaf in our tree */ |
213 |
|
for ( ; ; ) { |
214 |
|
for (q = 0; q < 4; q++) /* find any leaf this level */ |
215 |
< |
if (!(tp->flgs & BRF(q)) && tp->k[q].l != NULL) { |
216 |
< |
lp = tp->k[q].l; |
215 |
> |
if (tp->flgs & LFF(q)) { |
216 |
> |
li = tp->k[q].li; |
217 |
|
break; |
218 |
|
} |
219 |
|
q = 0; /* which quadrant are we? */ |
227 |
|
tp = tp->k[q].b; |
228 |
|
continue; |
229 |
|
} |
230 |
< |
if (tp->k[q].l != NULL) /* good shot! */ |
231 |
< |
return(tp->k[q].l); |
232 |
< |
return(lp); /* else return what we have */ |
230 |
> |
if (tp->flgs & LFF(q)) /* good shot! */ |
231 |
> |
return(tp->k[q].li); |
232 |
> |
return(li); /* else return what we have */ |
233 |
|
} |
234 |
|
} |
235 |
|
|
236 |
|
|
237 |
|
static |
238 |
< |
addleaf(lp) /* add a leaf to our tree */ |
239 |
< |
RLEAF *lp; |
238 |
> |
putleaf(li, drop) /* put a leaf in our tree */ |
239 |
> |
register int li; |
240 |
> |
int drop; |
241 |
|
{ |
242 |
|
register RTREE *tp = &qtrunk; |
243 |
|
int x0=0, y0=0, x1=odev.hres, y1=odev.vres; |
244 |
< |
RLEAF *lo = NULL; |
244 |
> |
register int lo = -1; |
245 |
> |
double d2; |
246 |
|
int x, y, mx, my; |
247 |
|
double z; |
248 |
< |
FVECT ip, wp; |
248 |
> |
FVECT ip, wp, vd; |
249 |
|
register int q; |
250 |
< |
/* compute leaf location */ |
251 |
< |
VCOPY(wp, lp->wp); |
250 |
> |
/* check for dead leaf */ |
251 |
> |
if (!qtL.chr[li][1] && !(qtL.chr[li][0] | qtL.chr[li][2])) |
252 |
> |
return(0); |
253 |
> |
/* compute leaf location in view */ |
254 |
> |
VCOPY(wp, qtL.wp[li]); |
255 |
|
viewloc(ip, &odev.v, wp); |
256 |
|
if (ip[2] <= 0. || ip[0] < 0. || ip[0] >= 1. |
257 |
|
|| ip[1] < 0. || ip[1] >= 1.) |
258 |
< |
return; |
258 |
> |
goto dropit; /* behind or outside view */ |
259 |
> |
#ifdef DEBUG |
260 |
> |
if (odev.v.type == VT_PAR | odev.v.vfore > FTINY) |
261 |
> |
error(INTERNAL, "bad view assumption in putleaf"); |
262 |
> |
#endif |
263 |
> |
for (q = 0; q < 3; q++) |
264 |
> |
vd[q] = (wp[q] - odev.v.vp[q])/ip[2]; |
265 |
> |
d2 = fdir2diff(qtL.wd[li], vd); |
266 |
> |
#ifdef MAXDIFF2 |
267 |
> |
if (d2 > MAXDIFF2) |
268 |
> |
goto dropit; /* leaf dir. too far off */ |
269 |
> |
#endif |
270 |
|
x = ip[0] * odev.hres; |
271 |
|
y = ip[1] * odev.vres; |
272 |
|
z = ip[2]; |
284 |
|
tp = tp->k[q].b; |
285 |
|
continue; |
286 |
|
} |
287 |
< |
if (tp->k[q].l == NULL) { /* found stem for leaf */ |
288 |
< |
tp->k[q].l = lp; |
289 |
< |
tp->flgs |= CHF(q); |
290 |
< |
break; |
287 |
> |
if (!(tp->flgs & LFF(q))) { /* found stem for leaf */ |
288 |
> |
tp->k[q].li = li; |
289 |
> |
tp->flgs |= CHLFF(q); |
290 |
> |
return(1); |
291 |
|
} |
292 |
< |
/* check existing leaf */ |
293 |
< |
if (lo != tp->k[q].l) { |
294 |
< |
lo = tp->k[q].l; |
263 |
< |
VCOPY(wp, lo->wp); |
292 |
> |
if (lo != tp->k[q].li) { /* check old leaf */ |
293 |
> |
lo = tp->k[q].li; |
294 |
> |
VCOPY(wp, qtL.wp[lo]); |
295 |
|
viewloc(ip, &odev.v, wp); |
296 |
|
} |
297 |
|
/* is node minimum size? */ |
298 |
< |
if (x1-x0 <= qtMinNodesiz || y1-y0 <= qtMinNodesiz) { |
299 |
< |
if (z > (1.-qtDepthEps)*ip[2]) /* who is closer? */ |
300 |
< |
return; /* old one is */ |
301 |
< |
tp->k[q].l = lp; /* new one is */ |
298 |
> |
if (y1-y0 <= qtMinNodesiz || x1-x0 <= qtMinNodesiz) { |
299 |
> |
if (z > (1.+qtDepthEps)*ip[2]) |
300 |
> |
break; /* old one closer */ |
301 |
> |
if (z >= (1.-qtDepthEps)*ip[2] && |
302 |
> |
fdir2diff(qtL.wd[lo], vd) < d2) |
303 |
> |
break; /* old one better */ |
304 |
> |
tp->k[q].li = li; /* attach new */ |
305 |
|
tp->flgs |= CHF(q); |
306 |
< |
tmAddHisto(&lo->brt, 1, -1); /* drop old one */ |
306 |
> |
li = lo; /* drop old... */ |
307 |
|
break; |
308 |
|
} |
309 |
< |
tp->flgs |= CHBRF(q); /* else grow tree */ |
309 |
> |
tp->flgs &= ~LFF(q); /* else grow tree */ |
310 |
> |
tp->flgs |= CHBRF(q); |
311 |
|
tp = tp->k[q].b = newtwig(); |
277 |
– |
tp->flgs |= CH_ANY; /* all new */ |
312 |
|
q = 0; /* old leaf -> new branch */ |
313 |
|
mx = ip[0] * odev.hres; |
314 |
|
my = ip[1] * odev.vres; |
315 |
|
if (mx >= (x0 + x1) >> 1) q |= 01; |
316 |
|
if (my >= (y0 + y1) >> 1) q |= 02; |
317 |
< |
tp->k[q].l = lo; |
317 |
> |
tp->flgs = CH_ANY|LFF(q); /* all new */ |
318 |
> |
tp->k[q].li = lo; |
319 |
|
} |
320 |
< |
tmAddHisto(&lp->brt, 1, 1); /* add leaf to histogram */ |
320 |
> |
dropit: |
321 |
> |
if (drop) { |
322 |
> |
qtL.chr[li][0] = qtL.chr[li][1] = qtL.chr[li][2] = 0; |
323 |
> |
qtL.wd[li] = falleaves; |
324 |
> |
falleaves = li; |
325 |
> |
} |
326 |
> |
return(li == lo); |
327 |
|
} |
328 |
|
|
329 |
|
|
330 |
< |
dev_value(c, p) /* add a pixel value to our output queue */ |
330 |
> |
dev_value(c, p, v) /* add a pixel value to our quadtree */ |
331 |
|
COLR c; |
332 |
< |
FVECT p; |
332 |
> |
FVECT p, v; |
333 |
|
{ |
334 |
< |
register RLEAF *lp; |
335 |
< |
|
336 |
< |
lp = newleaf(); |
337 |
< |
VCOPY(lp->wp, p); |
338 |
< |
tmCvColrs(&lp->brt, lp->chr, c, 1); |
339 |
< |
addleaf(lp); |
334 |
> |
register int li; |
335 |
> |
int mapit; |
336 |
> |
/* grab a leaf */ |
337 |
> |
if (falleaves >= 0) { /* check for fallen leaves */ |
338 |
> |
li = falleaves; |
339 |
> |
falleaves = qtL.wd[li]; |
340 |
> |
mapit = qtL.tml <= qtL.tl ? |
341 |
> |
(li < qtL.tml || li >= qtL.tl) : |
342 |
> |
(li < qtL.tml && li >= qtL.tl) ; |
343 |
> |
} else { /* else allocate new one */ |
344 |
> |
li = qtL.tl++; |
345 |
> |
if (qtL.tl >= qtL.nl) /* advance to next leaf in ring */ |
346 |
> |
qtL.tl = 0; |
347 |
> |
if (qtL.tl == qtL.bl) /* need to shake some free */ |
348 |
> |
qtCompost(LFREEPCT); |
349 |
> |
mapit = 0; /* we'll map it later */ |
350 |
> |
} |
351 |
> |
VCOPY(qtL.wp[li], p); |
352 |
> |
qtL.wd[li] = encodedir(v); |
353 |
> |
tmCvColrs(&qtL.brt[li], qtL.chr[li], c, 1); |
354 |
> |
if (putleaf(li, 1) && mapit) |
355 |
> |
tmMapPixels(qtL.rgb+li, qtL.brt+li, qtL.chr+li, 1); |
356 |
|
} |
357 |
|
|
358 |
|
|
359 |
|
qtReplant() /* replant our tree using new view */ |
360 |
|
{ |
361 |
|
register int i; |
362 |
< |
|
363 |
< |
if (bleaf == tleaf) /* anything to replant? */ |
362 |
> |
/* anything to replant? */ |
363 |
> |
if (qtL.bl == qtL.tl) |
364 |
|
return; |
365 |
< |
qtFreeTree(0); /* blow the tree away */ |
366 |
< |
/* now rebuild it */ |
367 |
< |
for (i = bleaf; i != tleaf; ) { |
368 |
< |
addleaf(leafpile+i); |
369 |
< |
if (++i >= nleaves) i = 0; |
365 |
> |
qtFreeTree(0); /* blow the old tree away */ |
366 |
> |
/* regrow it in new place */ |
367 |
> |
for (i = qtL.bl; i != qtL.tl; ) { |
368 |
> |
putleaf(i, 0); |
369 |
> |
if (++i >= qtL.nl) i = 0; |
370 |
|
} |
314 |
– |
tmComputeMapping(0., 0., 0.); /* update the display */ |
315 |
– |
qtUpdate(); |
371 |
|
} |
372 |
|
|
373 |
|
|
374 |
< |
static |
375 |
< |
redraw(ca, tp, x0, y0, x1, y1, l) /* redraw portion of a tree */ |
321 |
< |
BYTE ca[3]; /* returned average color */ |
322 |
< |
register RTREE *tp; |
323 |
< |
int x0, y0, x1, y1; |
324 |
< |
int l[2][2]; |
374 |
> |
qtMapLeaves(redo) /* map our leaves to RGB */ |
375 |
> |
int redo; |
376 |
|
{ |
377 |
< |
int csm[3], nc; |
378 |
< |
BYTE rgb[3]; |
379 |
< |
int quads = CH_ANY; |
380 |
< |
int mx, my; |
381 |
< |
register int i; |
382 |
< |
/* compute midpoint */ |
383 |
< |
mx = (x0 + x1) >> 1; |
384 |
< |
my = (y0 + y1) >> 1; |
385 |
< |
/* see what to do */ |
386 |
< |
if (l[0][0] >= mx) |
387 |
< |
quads &= ~(CHF(2)|CHF(0)); |
388 |
< |
else if (l[0][1] <= mx) |
338 |
< |
quads &= ~(CHF(3)|CHF(1)); |
339 |
< |
if (l[1][0] >= my) |
340 |
< |
quads &= ~(CHF(1)|CHF(0)); |
341 |
< |
else if (l[1][1] <= my) |
342 |
< |
quads &= ~(CHF(3)|CHF(2)); |
343 |
< |
tp->flgs &= ~quads; /* mark them done */ |
344 |
< |
csm[0] = csm[1] = csm[2] = nc = 0; |
345 |
< |
/* do leaves first */ |
346 |
< |
for (i = 0; i < 4; i++) |
347 |
< |
if (quads & CHF(i) && !(tp->flgs & BRF(i)) && |
348 |
< |
tp->k[i].l != NULL) { |
349 |
< |
tmMapPixels(rgb, &tp->k[i].l->brt, tp->k[i].l->chr, 1); |
350 |
< |
dev_paintr(rgb, i&01 ? mx : x0, i&02 ? my : y0, |
351 |
< |
i&01 ? x1 : mx, i&02 ? y1 : my); |
352 |
< |
csm[0] += rgb[0]; csm[1] += rgb[1]; csm[2] += rgb[2]; |
353 |
< |
nc++; |
354 |
< |
quads &= ~CHF(i); |
355 |
< |
} |
356 |
< |
/* now do branches */ |
357 |
< |
for (i = 0; i < 4; i++) |
358 |
< |
if (quads & CHF(i) && tp->flgs & BRF(i)) { |
359 |
< |
redraw(rgb, tp->k[i].b, i&01 ? mx : x0, i&02 ? my : y0, |
360 |
< |
i&01 ? x1 : mx, i&02 ? y1 : my, l); |
361 |
< |
csm[0] += rgb[0]; csm[1] += rgb[1]; csm[2] += rgb[2]; |
362 |
< |
nc++; |
363 |
< |
quads &= ~CHF(i); |
364 |
< |
} |
365 |
< |
if (nc > 1) { |
366 |
< |
ca[0] = csm[0]/nc; ca[1] = csm[1]/nc; ca[2] = csm[2]/nc; |
377 |
> |
int aorg, alen, borg, blen; |
378 |
> |
/* recompute mapping? */ |
379 |
> |
if (redo) |
380 |
> |
qtL.tml = qtL.bl; |
381 |
> |
/* already done? */ |
382 |
> |
if (qtL.tml == qtL.tl) |
383 |
> |
return(1); |
384 |
> |
/* compute segments */ |
385 |
> |
aorg = qtL.tml; |
386 |
> |
if (qtL.tl >= aorg) { |
387 |
> |
alen = qtL.tl - aorg; |
388 |
> |
blen = 0; |
389 |
|
} else { |
390 |
< |
ca[0] = csm[0]; ca[1] = csm[1]; ca[2] = csm[2]; |
390 |
> |
alen = qtL.nl - aorg; |
391 |
> |
borg = 0; |
392 |
> |
blen = qtL.tl; |
393 |
|
} |
394 |
< |
if (!quads) return; |
395 |
< |
/* fill in gaps with average */ |
396 |
< |
for (i = 0; i < 4; i++) |
397 |
< |
if (quads & CHF(i)) |
398 |
< |
dev_paintr(ca, i&01 ? mx : x0, i&02 ? my : y0, |
399 |
< |
i&01 ? x1 : mx, i&02 ? y1 : my); |
376 |
< |
} |
377 |
< |
|
378 |
< |
|
379 |
< |
static |
380 |
< |
update(ca, tp, x0, y0, x1, y1) /* update tree display as needed */ |
381 |
< |
BYTE ca[3]; /* returned average color */ |
382 |
< |
register RTREE *tp; |
383 |
< |
int x0, y0, x1, y1; |
384 |
< |
{ |
385 |
< |
int csm[3], nc; |
386 |
< |
BYTE rgb[3]; |
387 |
< |
int gaps = 0; |
388 |
< |
int mx, my; |
389 |
< |
register int i; |
390 |
< |
/* compute midpoint */ |
391 |
< |
mx = (x0 + x1) >> 1; |
392 |
< |
my = (y0 + y1) >> 1; |
393 |
< |
csm[0] = csm[1] = csm[2] = nc = 0; |
394 |
< |
/* do leaves first */ |
395 |
< |
for (i = 0; i < 4; i++) |
396 |
< |
if ((tp->flgs & CHBRF(i)) == CHF(i)) { |
397 |
< |
if (tp->k[i].l == NULL) { |
398 |
< |
gaps |= 1<<i; /* empty stem */ |
399 |
< |
continue; |
400 |
< |
} |
401 |
< |
tmMapPixels(rgb, &tp->k[i].l->brt, tp->k[i].l->chr, 1); |
402 |
< |
dev_paintr(rgb, i&01 ? mx : x0, i&02 ? my : y0, |
403 |
< |
i&01 ? x1 : mx, i&02 ? y1 : my); |
404 |
< |
csm[0] += rgb[0]; csm[1] += rgb[1]; csm[2] += rgb[2]; |
405 |
< |
nc++; |
406 |
< |
} |
407 |
< |
/* now do branches */ |
408 |
< |
for (i = 0; i < 4; i++) |
409 |
< |
if ((tp->flgs & CHBRF(i)) == CHBRF(i)) { |
410 |
< |
update(rgb, tp->k[i].b, i&01 ? mx : x0, i&02 ? my : y0, |
411 |
< |
i&01 ? x1 : mx, i&02 ? y1 : my); |
412 |
< |
csm[0] += rgb[0]; csm[1] += rgb[1]; csm[2] += rgb[2]; |
413 |
< |
nc++; |
414 |
< |
} |
415 |
< |
if (nc > 1) { |
416 |
< |
ca[0] = csm[0]/nc; ca[1] = csm[1]/nc; ca[2] = csm[2]/nc; |
417 |
< |
} else { |
418 |
< |
ca[0] = csm[0]; ca[1] = csm[1]; ca[2] = csm[2]; |
419 |
< |
} |
420 |
< |
/* fill in gaps with average */ |
421 |
< |
for (i = 0; gaps && i < 4; gaps >>= 1, i++) |
422 |
< |
if (gaps & 01) |
423 |
< |
dev_paintr(ca, i&01 ? mx : x0, i&02 ? my : y0, |
424 |
< |
i&01 ? x1 : mx, i&02 ? y1 : my); |
425 |
< |
tp->flgs &= ~CH_ANY; /* all done */ |
426 |
< |
} |
427 |
< |
|
428 |
< |
|
429 |
< |
qtRedraw(x0, y0, x1, y1) /* redraw part of our screen */ |
430 |
< |
int x0, y0, x1, y1; |
431 |
< |
{ |
432 |
< |
int lim[2][2]; |
433 |
< |
BYTE ca[3]; |
434 |
< |
|
435 |
< |
if (is_stump(&qtrunk)) |
436 |
< |
return; |
437 |
< |
if ((lim[0][0]=x0) <= 0 & (lim[1][0]=y0) <= 0 & |
438 |
< |
(lim[0][1]=x1) >= odev.hres-1 & (lim[1][1]=y1) >= odev.vres-1 |
439 |
< |
|| tmTop->lumap == NULL) |
394 |
> |
/* (re)compute tone mapping? */ |
395 |
> |
if (qtL.tml == qtL.bl) { |
396 |
> |
tmClearHisto(); |
397 |
> |
tmAddHisto(qtL.brt+aorg, alen, 1); |
398 |
> |
if (blen > 0) |
399 |
> |
tmAddHisto(qtL.brt+borg, blen, 1); |
400 |
|
if (tmComputeMapping(0., 0., 0.) != TM_E_OK) |
401 |
< |
return; |
402 |
< |
redraw(ca, &qtrunk, 0, 0, odev.hres, odev.vres, lim); |
403 |
< |
} |
404 |
< |
|
405 |
< |
|
406 |
< |
qtUpdate() /* update our tree display */ |
407 |
< |
{ |
408 |
< |
BYTE ca[3]; |
409 |
< |
|
410 |
< |
if (is_stump(&qtrunk)) |
451 |
< |
return; |
452 |
< |
if (tmTop->lumap == NULL) |
453 |
< |
tmComputeMapping(0., 0., 0.); |
454 |
< |
update(ca, &qtrunk, 0, 0, odev.hres, odev.vres); |
401 |
> |
return(0); |
402 |
> |
} |
403 |
> |
if (tmMapPixels(qtL.rgb+aorg, qtL.brt+aorg, |
404 |
> |
qtL.chr+aorg, alen) != TM_E_OK) |
405 |
> |
return(0); |
406 |
> |
if (blen > 0) |
407 |
> |
tmMapPixels(qtL.rgb+borg, qtL.brt+borg, |
408 |
> |
qtL.chr+borg, blen); |
409 |
> |
qtL.tml = qtL.tl; |
410 |
> |
return(1); |
411 |
|
} |