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 |
|
|
22 |
+ |
#define MAXDIFF2 (long)( MAXANG*MAXANG /90./90.*(1L<<15)*(1L<<15)) |
23 |
+ |
|
24 |
+ |
#define abs(i) ((i) < 0 ? -(i) : (i)) |
25 |
+ |
|
26 |
|
RTREE qtrunk; /* our quadtree trunk */ |
27 |
< |
double qtDepthEps = .02; /* epsilon to compare depths (z fraction) */ |
27 |
> |
double qtDepthEps = .05; /* epsilon to compare depths (z fraction) */ |
28 |
|
int qtMinNodesiz = 2; /* minimum node dimension (pixels) */ |
29 |
+ |
struct rleaves qtL; /* our pile of leaves */ |
30 |
|
|
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 |
– |
|
31 |
|
#define TBUNDLESIZ 409 /* number of twigs in a bundle */ |
32 |
|
|
33 |
|
static RTREE **twigbundle; /* free twig blocks (NULL term.) */ |
34 |
|
static int nexttwig; /* next free twig */ |
35 |
|
|
36 |
< |
static RTREE emptytree; /* empty tree for test below */ |
36 |
> |
#define ungetleaf(li) (qtL.tl=(li)) /* dangerous if used improperly */ |
37 |
|
|
29 |
– |
#define is_stump(t) (!bcmp((char *)(t), (char *)&emptytree, sizeof(RTREE))) |
38 |
|
|
31 |
– |
|
39 |
|
static RTREE * |
40 |
|
newtwig() /* allocate a twig */ |
41 |
|
{ |
70 |
|
{ |
71 |
|
register int i; |
72 |
|
|
73 |
< |
if (tmTop != NULL) |
67 |
< |
tmClearHisto(); |
68 |
< |
bzero((char *)&qtrunk, sizeof(RTREE)); |
69 |
< |
nexttwig = 0; |
73 |
> |
qtrunk.flgs = CH_ANY; /* chop down tree */ |
74 |
|
if (twigbundle == NULL) |
75 |
|
return; |
76 |
+ |
i = (TBUNDLESIZ-1+nexttwig)/TBUNDLESIZ; |
77 |
+ |
nexttwig = 0; |
78 |
|
if (!really) { /* just clear allocated blocks */ |
79 |
< |
for (i = 0; twigbundle[i] != NULL; i++) |
79 |
> |
while (i--) |
80 |
|
bzero((char *)twigbundle[i], TBUNDLESIZ*sizeof(RTREE)); |
81 |
|
return; |
82 |
|
} |
88 |
|
} |
89 |
|
|
90 |
|
|
91 |
< |
static RLEAF * |
91 |
> |
static int |
92 |
|
newleaf() /* allocate a leaf from our pile */ |
93 |
|
{ |
94 |
< |
if (tleaf++ >= nleaves) /* get next leaf in ring */ |
95 |
< |
tleaf = 0; |
96 |
< |
if (tleaf == bleaf) /* need to shake some free */ |
94 |
> |
int li; |
95 |
> |
|
96 |
> |
li = qtL.tl++; |
97 |
> |
if (qtL.tl >= qtL.nl) /* get next leaf in ring */ |
98 |
> |
qtL.tl = 0; |
99 |
> |
if (qtL.tl == qtL.bl) /* need to shake some free */ |
100 |
|
qtCompost(LFREEPCT); |
101 |
< |
return(leafpile + tleaf); |
101 |
> |
return(li); |
102 |
|
} |
103 |
|
|
104 |
|
|
105 |
+ |
#define LEAFSIZ (3*sizeof(float)+2*sizeof(short)+\ |
106 |
+ |
sizeof(TMbright)+6*sizeof(BYTE)) |
107 |
+ |
|
108 |
|
int |
109 |
|
qtAllocLeaves(n) /* allocate space for n leaves */ |
110 |
< |
int n; |
110 |
> |
register int n; |
111 |
|
{ |
112 |
|
unsigned nbytes; |
113 |
|
register unsigned i; |
115 |
|
qtFreeTree(0); /* make sure tree is empty */ |
116 |
|
if (n <= 0) |
117 |
|
return(0); |
118 |
< |
if (nleaves >= n) |
119 |
< |
return(nleaves); |
120 |
< |
else if (nleaves > 0) |
121 |
< |
free((char *)leafpile); |
118 |
> |
if (qtL.nl >= n) |
119 |
> |
return(qtL.nl); |
120 |
> |
else if (qtL.nl > 0) |
121 |
> |
free(qtL.base); |
122 |
|
/* round space up to nearest power of 2 */ |
123 |
< |
nbytes = n*sizeof(RLEAF) + 8; |
123 |
> |
nbytes = n*LEAFSIZ + 8; |
124 |
|
for (i = 1024; nbytes > i; i <<= 1) |
125 |
|
; |
126 |
< |
n = (i - 8) / sizeof(RLEAF); |
127 |
< |
leafpile = (RLEAF *)malloc(n*sizeof(RLEAF)); |
128 |
< |
if (leafpile == NULL) |
129 |
< |
return(-1); |
130 |
< |
nleaves = n; |
131 |
< |
bleaf = tleaf = 0; |
132 |
< |
return(nleaves); |
126 |
> |
n = (i - 8) / LEAFSIZ; /* should we make sure n is even? */ |
127 |
> |
qtL.base = (char *)malloc(n*LEAFSIZ); |
128 |
> |
if (qtL.base == NULL) |
129 |
> |
return(0); |
130 |
> |
/* assign larger alignment types earlier */ |
131 |
> |
qtL.wp = (float (*)[3])qtL.base; |
132 |
> |
qtL.wd = (short (*)[2])(qtL.wp + n); |
133 |
> |
qtL.brt = (TMbright *)(qtL.wd + n); |
134 |
> |
qtL.chr = (BYTE (*)[3])(qtL.brt + n); |
135 |
> |
qtL.rgb = (BYTE (*)[3])(qtL.chr + n); |
136 |
> |
qtL.nl = n; |
137 |
> |
qtL.tml = qtL.bl = qtL.tl = 0; |
138 |
> |
return(n); |
139 |
|
} |
140 |
|
|
141 |
+ |
#undef LEAFSIZ |
142 |
|
|
143 |
+ |
|
144 |
|
qtFreeLeaves() /* free our allocated leaves and twigs */ |
145 |
|
{ |
146 |
|
qtFreeTree(1); /* free tree also */ |
147 |
< |
if (nleaves <= 0) |
147 |
> |
if (qtL.nl <= 0) |
148 |
|
return; |
149 |
< |
free((char *)leafpile); |
150 |
< |
leafpile = NULL; |
151 |
< |
nleaves = 0; |
149 |
> |
free(qtL.base); |
150 |
> |
qtL.base = NULL; |
151 |
> |
qtL.nl = 0; |
152 |
|
} |
153 |
|
|
154 |
|
|
159 |
|
register int i, li; |
160 |
|
|
161 |
|
for (i = 0; i < 4; i++) |
162 |
< |
if (tp->flgs & BRF(i)) |
162 |
> |
if (tp->flgs & BRF(i)) { |
163 |
|
shaketree(tp->k[i].b); |
164 |
< |
else if (tp->k[i].l != NULL) { |
165 |
< |
li = tp->k[i].l - leafpile; |
166 |
< |
if (bleaf < tleaf ? (li < bleaf || li >= tleaf) : |
167 |
< |
(li < bleaf && li >= tleaf)) { |
168 |
< |
tmAddHisto(&tp->k[i].l->brt, 1, -1); |
169 |
< |
tp->k[i].l = NULL; |
170 |
< |
} |
164 |
> |
if (is_stump(tp->k[i].b)) |
165 |
> |
tp->flgs &= ~BRF(i); |
166 |
> |
} else if (tp->flgs & LFF(i)) { |
167 |
> |
li = tp->k[i].li; |
168 |
> |
if (qtL.bl < qtL.tl ? |
169 |
> |
(li < qtL.bl || li >= qtL.tl) : |
170 |
> |
(li < qtL.bl && li >= qtL.tl)) |
171 |
> |
tp->flgs &= ~LFF(i); |
172 |
|
} |
173 |
|
} |
174 |
|
|
177 |
|
qtCompost(pct) /* free up some leaves */ |
178 |
|
int pct; |
179 |
|
{ |
180 |
< |
int nused, nclear; |
180 |
> |
int nused, nclear, nmapped; |
181 |
> |
|
182 |
|
/* figure out how many leaves to clear */ |
183 |
< |
nclear = nleaves * pct / 100; |
183 |
> |
nclear = qtL.nl * pct / 100; |
184 |
> |
nused = qtL.tl - qtL.bl; |
185 |
> |
if (nused <= 0) nused += qtL.nl; |
186 |
> |
nclear -= qtL.nl - nused; |
187 |
|
if (nclear <= 0) |
188 |
|
return(0); |
164 |
– |
nused = tleaf > bleaf ? tleaf-bleaf : tleaf+nleaves-bleaf; |
189 |
|
if (nclear >= nused) { /* clear them all */ |
190 |
|
qtFreeTree(0); |
191 |
< |
bleaf = tleaf = 0; |
191 |
> |
qtL.tml = qtL.bl = qtL.tl = 0; |
192 |
|
return(nused); |
193 |
|
} |
194 |
|
/* else clear leaves from bottom */ |
195 |
< |
bleaf = (bleaf + nclear) % nleaves; |
195 |
> |
nmapped = qtL.tml - qtL.bl; |
196 |
> |
if (nmapped < 0) nmapped += qtL.nl; |
197 |
> |
qtL.bl += nclear; |
198 |
> |
if (qtL.bl >= qtL.nl) qtL.bl -= qtL.nl; |
199 |
> |
if (nmapped <= nclear) qtL.tml = qtL.bl; |
200 |
|
shaketree(&qtrunk); |
201 |
|
return(nclear); |
202 |
|
} |
203 |
|
|
204 |
|
|
205 |
< |
RLEAF * |
205 |
> |
static |
206 |
> |
encodedir(pa, dv) /* encode a normalized direction vector */ |
207 |
> |
short pa[2]; |
208 |
> |
FVECT dv; |
209 |
> |
{ |
210 |
> |
pa[1] = 0; |
211 |
> |
if (dv[2] >= 1.) |
212 |
> |
pa[0] = (1L<<15)-1; |
213 |
> |
else if (dv[2] <= -1.) |
214 |
> |
pa[0] = -((1L<<15)-1); |
215 |
> |
else { |
216 |
> |
pa[0] = ((1L<<15)-1)/(PI/2.) * asin(dv[2]); |
217 |
> |
pa[1] = ((1L<<15)-1)/PI * atan2(dv[1], dv[0]); |
218 |
> |
} |
219 |
> |
} |
220 |
> |
|
221 |
> |
|
222 |
> |
#define ALTSHFT 5 |
223 |
> |
#define NALT (1<<ALTSHFT) |
224 |
> |
#define azisft(alt) azisftab[abs(alt)>>(15-ALTSHFT)] |
225 |
> |
|
226 |
> |
static unsigned short azisftab[NALT]; |
227 |
> |
|
228 |
> |
static |
229 |
> |
azisftinit(alt) /* initialize azimuth scale factor table */ |
230 |
> |
int alt; |
231 |
> |
{ |
232 |
> |
register int i; |
233 |
> |
|
234 |
> |
for (i = NALT; i--; ) |
235 |
> |
azisftab[i] = 2.*(1L<<15) * cos(PI/2.*(i+.5)/NALT); |
236 |
> |
return(azisft(alt)); |
237 |
> |
} |
238 |
> |
|
239 |
> |
#define azisf(alt) (azisftab[0] ? azisft(alt) : azisftinit(alt)) >> 15 |
240 |
> |
|
241 |
> |
static long |
242 |
> |
dir2diff(pa1, pa2) /* relative distance^2 between directions */ |
243 |
> |
short pa1[2], pa2[2]; |
244 |
> |
{ |
245 |
> |
long altd2, azid2; |
246 |
> |
int alt; |
247 |
> |
|
248 |
> |
altd2 = pa1[0] - pa2[0]; /* get altitude difference^2 */ |
249 |
> |
altd2 *= altd2; |
250 |
> |
if (altd2 > MAXDIFF2) |
251 |
> |
return(altd2); /* too large already */ |
252 |
> |
azid2 = pa1[1] - pa2[1]; /* get adjusted azimuth difference^2 */ |
253 |
> |
if (azid2 < 0) azid2 = -azid2; |
254 |
> |
if (azid2 >= 1L<<15) { /* wrap sphere */ |
255 |
> |
azid2 -= 1L<<16; |
256 |
> |
if (azid2 < 0) azid2 = -azid2; |
257 |
> |
} |
258 |
> |
alt = (pa1[0]+pa2[0])/2; |
259 |
> |
azid2 = azid2*azisf(alt); /* evaluation order is important */ |
260 |
> |
azid2 *= azid2; |
261 |
> |
return(altd2 + azid2); |
262 |
> |
} |
263 |
> |
|
264 |
> |
|
265 |
> |
int |
266 |
|
qtFindLeaf(x, y) /* find closest leaf to (x,y) */ |
267 |
|
int x, y; |
268 |
|
{ |
269 |
|
register RTREE *tp = &qtrunk; |
270 |
< |
RLEAF *lp = NULL; |
270 |
> |
int li = -1; |
271 |
|
int x0=0, y0=0, x1=odev.hres, y1=odev.vres; |
272 |
|
int mx, my; |
273 |
|
register int q; |
274 |
|
/* check limits */ |
275 |
|
if (x < 0 || x >= odev.hres || y < 0 || y >= odev.vres) |
276 |
< |
return(NULL); |
276 |
> |
return(-1); |
277 |
|
/* find nearby leaf in our tree */ |
278 |
|
for ( ; ; ) { |
279 |
|
for (q = 0; q < 4; q++) /* find any leaf this level */ |
280 |
< |
if (!(tp->flgs & BRF(q)) && tp->k[q].l != NULL) { |
281 |
< |
lp = tp->k[q].l; |
280 |
> |
if (tp->flgs & LFF(q)) { |
281 |
> |
li = tp->k[q].li; |
282 |
|
break; |
283 |
|
} |
284 |
|
q = 0; /* which quadrant are we? */ |
292 |
|
tp = tp->k[q].b; |
293 |
|
continue; |
294 |
|
} |
295 |
< |
if (tp->k[q].l != NULL) /* good shot! */ |
296 |
< |
return(tp->k[q].l); |
297 |
< |
return(lp); /* else return what we have */ |
295 |
> |
if (tp->flgs & LFF(q)) /* good shot! */ |
296 |
> |
return(tp->k[q].li); |
297 |
> |
return(li); /* else return what we have */ |
298 |
|
} |
299 |
|
} |
300 |
|
|
301 |
|
|
302 |
|
static |
303 |
< |
addleaf(lp) /* add a leaf to our tree */ |
304 |
< |
RLEAF *lp; |
303 |
> |
addleaf(li) /* add a leaf to our tree */ |
304 |
> |
int li; |
305 |
|
{ |
306 |
|
register RTREE *tp = &qtrunk; |
307 |
|
int x0=0, y0=0, x1=odev.hres, y1=odev.vres; |
308 |
< |
RLEAF *lo = NULL; |
308 |
> |
int lo = -1; |
309 |
> |
long d2; |
310 |
> |
short dc[2]; |
311 |
|
int x, y, mx, my; |
312 |
|
double z; |
313 |
|
FVECT ip, wp; |
314 |
|
register int q; |
315 |
< |
/* compute leaf location */ |
316 |
< |
VCOPY(wp, lp->wp); |
315 |
> |
/* compute leaf location in view */ |
316 |
> |
VCOPY(wp, qtL.wp[li]); |
317 |
|
viewloc(ip, &odev.v, wp); |
318 |
|
if (ip[2] <= 0. || ip[0] < 0. || ip[0] >= 1. |
319 |
|
|| ip[1] < 0. || ip[1] >= 1.) |
320 |
< |
return; |
320 |
> |
return(0); /* behind or outside view */ |
321 |
> |
#ifdef DEBUG |
322 |
> |
if (odev.v.type == VT_PAR | odev.v.vfore > FTINY) |
323 |
> |
error(INTERNAL, "bad view assumption in addleaf"); |
324 |
> |
#endif |
325 |
> |
for (q = 0; q < 3; q++) |
326 |
> |
wp[q] = (wp[q] - odev.v.vp[q])/ip[2]; |
327 |
> |
encodedir(dc, wp); /* compute pixel direction */ |
328 |
> |
d2 = dir2diff(dc, qtL.wd[li]); |
329 |
> |
if (d2 > MAXDIFF2) |
330 |
> |
return(0); /* leaf dir. too far off */ |
331 |
|
x = ip[0] * odev.hres; |
332 |
|
y = ip[1] * odev.vres; |
333 |
|
z = ip[2]; |
345 |
|
tp = tp->k[q].b; |
346 |
|
continue; |
347 |
|
} |
348 |
< |
if (tp->k[q].l == NULL) { /* found stem for leaf */ |
349 |
< |
tp->k[q].l = lp; |
350 |
< |
tp->flgs |= CHF(q); |
348 |
> |
if (!(tp->flgs & LFF(q))) { /* found stem for leaf */ |
349 |
> |
tp->k[q].li = li; |
350 |
> |
tp->flgs |= CHLFF(q); |
351 |
|
break; |
352 |
|
} |
353 |
< |
/* check existing leaf */ |
354 |
< |
if (lo != tp->k[q].l) { |
355 |
< |
lo = tp->k[q].l; |
256 |
< |
VCOPY(wp, lo->wp); |
353 |
> |
if (lo != tp->k[q].li) { /* check old leaf */ |
354 |
> |
lo = tp->k[q].li; |
355 |
> |
VCOPY(wp, qtL.wp[lo]); |
356 |
|
viewloc(ip, &odev.v, wp); |
357 |
|
} |
358 |
|
/* is node minimum size? */ |
359 |
< |
if (x1-x0 <= qtMinNodesiz || y1-y0 <= qtMinNodesiz) { |
360 |
< |
if (z > (1.-qtDepthEps)*ip[2]) /* who is closer? */ |
361 |
< |
return; /* old one is */ |
362 |
< |
tp->k[q].l = lp; /* new one is */ |
359 |
> |
if (y1-y0 <= qtMinNodesiz || x1-x0 <= qtMinNodesiz) { |
360 |
> |
if (z > (1.+qtDepthEps)*ip[2]) |
361 |
> |
return(0); /* old one closer */ |
362 |
> |
if (z >= (1.-qtDepthEps)*ip[2] && |
363 |
> |
dir2diff(dc, qtL.wd[lo]) < d2) |
364 |
> |
return(0); /* old one better */ |
365 |
> |
tp->k[q].li = li; /* else new one is */ |
366 |
|
tp->flgs |= CHF(q); |
265 |
– |
tmAddHisto(&lo->brt, 1, -1); /* drop old one */ |
367 |
|
break; |
368 |
|
} |
369 |
< |
tp->flgs |= CHBRF(q); /* else grow tree */ |
369 |
> |
tp->flgs &= ~LFF(q); /* else grow tree */ |
370 |
> |
tp->flgs |= CHBRF(q); |
371 |
|
tp = tp->k[q].b = newtwig(); |
270 |
– |
tp->flgs |= CH_ANY; /* all new */ |
372 |
|
q = 0; /* old leaf -> new branch */ |
373 |
|
mx = ip[0] * odev.hres; |
374 |
|
my = ip[1] * odev.vres; |
375 |
|
if (mx >= (x0 + x1) >> 1) q |= 01; |
376 |
|
if (my >= (y0 + y1) >> 1) q |= 02; |
377 |
< |
tp->k[q].l = lo; |
377 |
> |
tp->k[q].li = lo; |
378 |
> |
tp->flgs |= LFF(q)|CH_ANY; /* all new */ |
379 |
|
} |
380 |
< |
tmAddHisto(&lp->brt, 1, 1); /* add leaf to histogram */ |
380 |
> |
return(1); /* done */ |
381 |
|
} |
382 |
|
|
383 |
|
|
384 |
< |
dev_value(c, p) /* add a pixel value to our output queue */ |
384 |
> |
dev_value(c, p, v) /* add a pixel value to our quadtree */ |
385 |
|
COLR c; |
386 |
< |
FVECT p; |
386 |
> |
FVECT p, v; |
387 |
|
{ |
388 |
< |
register RLEAF *lp; |
388 |
> |
register int li; |
389 |
|
|
390 |
< |
lp = newleaf(); |
391 |
< |
VCOPY(lp->wp, p); |
392 |
< |
tmCvColrs(&lp->brt, lp->chr, c, 1); |
393 |
< |
addleaf(lp); |
390 |
> |
li = newleaf(); |
391 |
> |
VCOPY(qtL.wp[li], p); |
392 |
> |
encodedir(qtL.wd[li], v); |
393 |
> |
tmCvColrs(&qtL.brt[li], qtL.chr[li], c, 1); |
394 |
> |
if (!addleaf(li)) |
395 |
> |
ungetleaf(li); |
396 |
|
} |
397 |
|
|
398 |
|
|
399 |
|
qtReplant() /* replant our tree using new view */ |
400 |
|
{ |
401 |
|
register int i; |
402 |
< |
|
403 |
< |
if (bleaf == tleaf) /* anything to replant? */ |
402 |
> |
/* anything to replant? */ |
403 |
> |
if (qtL.bl == qtL.tl) |
404 |
|
return; |
405 |
< |
qtFreeTree(0); /* blow the tree away */ |
406 |
< |
/* now rebuild it */ |
407 |
< |
for (i = bleaf; i != tleaf; ) { |
408 |
< |
addleaf(leafpile+i); |
409 |
< |
if (++i >= nleaves) i = 0; |
405 |
> |
qtFreeTree(0); /* blow the old tree away */ |
406 |
> |
/* regrow it in new place */ |
407 |
> |
for (i = qtL.bl; i != qtL.tl; ) { |
408 |
> |
addleaf(i); |
409 |
> |
if (++i >= qtL.nl) i = 0; |
410 |
|
} |
307 |
– |
tmComputeMapping(0., 0., 0.); /* update the display */ |
308 |
– |
qtUpdate(); |
411 |
|
} |
412 |
|
|
413 |
|
|
414 |
< |
static |
415 |
< |
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]; |
414 |
> |
qtMapLeaves(redo) /* map our leaves to RGB */ |
415 |
> |
int redo; |
416 |
|
{ |
417 |
< |
int csm[3], nc; |
418 |
< |
BYTE rgb[3]; |
419 |
< |
int quads = CH_ANY; |
420 |
< |
int mx, my; |
421 |
< |
register int i; |
422 |
< |
/* compute midpoint */ |
423 |
< |
mx = (x0 + x1) >> 1; |
424 |
< |
my = (y0 + y1) >> 1; |
425 |
< |
/* see what to do */ |
426 |
< |
if (l[0][0] >= mx) |
427 |
< |
quads &= ~(CHF(2)|CHF(0)); |
428 |
< |
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; |
417 |
> |
int aorg, alen, borg, blen; |
418 |
> |
/* recompute mapping? */ |
419 |
> |
if (redo) |
420 |
> |
qtL.tml = qtL.bl; |
421 |
> |
/* already done? */ |
422 |
> |
if (qtL.tml == qtL.tl) |
423 |
> |
return(1); |
424 |
> |
/* compute segments */ |
425 |
> |
aorg = qtL.tml; |
426 |
> |
if (qtL.tl >= aorg) { |
427 |
> |
alen = qtL.tl - aorg; |
428 |
> |
blen = 0; |
429 |
|
} else { |
430 |
< |
ca[0] = csm[0]; ca[1] = csm[1]; ca[2] = csm[2]; |
430 |
> |
alen = qtL.nl - aorg; |
431 |
> |
borg = 0; |
432 |
> |
blen = qtL.tl; |
433 |
|
} |
434 |
< |
if (!quads) return; |
435 |
< |
/* fill in gaps with average */ |
436 |
< |
for (i = 0; i < 4; i++) |
437 |
< |
if (quads & CHF(i)) |
438 |
< |
dev_paintr(ca, i&01 ? mx : x0, i&02 ? my : y0, |
439 |
< |
i&01 ? x1 : mx, i&02 ? y1 : my); |
440 |
< |
} |
441 |
< |
|
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]; |
434 |
> |
/* (re)compute tone mapping? */ |
435 |
> |
if (qtL.tml == qtL.bl) { |
436 |
> |
tmClearHisto(); |
437 |
> |
tmAddHisto(qtL.brt+aorg, alen, 1); |
438 |
> |
if (blen > 0) |
439 |
> |
tmAddHisto(qtL.brt+borg, blen, 1); |
440 |
> |
if (tmComputeMapping(0., 0., 0.) != TM_E_OK) |
441 |
> |
return(0); |
442 |
|
} |
443 |
< |
/* fill in gaps with average */ |
444 |
< |
for (i = 0; gaps && i < 4; gaps >>= 1, i++) |
445 |
< |
if (gaps & 01) |
446 |
< |
dev_paintr(ca, i&01 ? mx : x0, i&02 ? my : y0, |
447 |
< |
i&01 ? x1 : mx, i&02 ? y1 : my); |
448 |
< |
tp->flgs &= ~CH_ANY; /* all done */ |
449 |
< |
} |
450 |
< |
|
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); |
443 |
> |
if (tmMapPixels(qtL.rgb+aorg, qtL.brt+aorg, |
444 |
> |
qtL.chr+aorg, alen) != TM_E_OK) |
445 |
> |
return(0); |
446 |
> |
if (blen > 0) |
447 |
> |
tmMapPixels(qtL.rgb+borg, qtL.brt+borg, |
448 |
> |
qtL.chr+borg, blen); |
449 |
> |
qtL.tml = qtL.tl; |
450 |
> |
return(1); |
451 |
|
} |