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