ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhd_qtree.c
(Generate patch)

Comparing ray/src/hd/rhd_qtree.c (file contents):
Revision 3.2 by gregl, Thu Nov 20 11:38:26 1997 UTC vs.
Revision 3.23 by schorsch, Mon Jun 30 14:59:11 2003 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines