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