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.3 by gregl, Thu Nov 20 18:03:43 1997 UTC vs.
Revision 3.24 by schorsch, Mon Jul 21 22:30:18 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 63 | Line 76 | int    really;
76   {
77          register int    i;
78  
79 <        if (tmTop != NULL)
67 <                tmClearHisto();
68 <        bzero((char *)&qtrunk, sizeof(RTREE));
69 <        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 */
87 < {
88 <        if (tleaf++ >= nleaves)         /* get next leaf in ring */
89 <                tleaf = 0;
90 <        if (tleaf == bleaf)             /* need to shake some free */
91 <                qtCompost(LFREEPCT);
92 <        return(leafpile + tleaf);
93 < }
97 > #define LEAFSIZ         (3*sizeof(float)+sizeof(int32)+\
98 >                        sizeof(TMbright)+6*sizeof(BYTE))
99  
95
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;
# Line 103 | Line 107 | int    n;
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          qtFreeTree(1);          /* free tree also */
140 <        if (nleaves <= 0)
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 139 | 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;
150 <                        }
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 156 | 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);
164        nused = tleaf > bleaf ? tleaf-bleaf : tleaf+nleaves-bleaf;
180          if (nclear >= nused) {  /* clear them all */
181                  qtFreeTree(0);
182 <                bleaf = tleaf = 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 < RLEAF *
201 > int
202   qtFindLeaf(x, y)                /* find closest leaf to (x,y) */
203   int     x, y;
204   {
205          register RTREE  *tp = &qtrunk;
206 <        RLEAF   *lp = NULL;
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(NULL);
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 & BRF(q)) && tp->k[q].l != NULL) {
217 <                                lp = tp->k[q].l;
216 >                        if (tp->flgs & LFF(q)) {
217 >                                li = tp->k[q].li;
218                                  break;
219                          }
220                  q = 0;                          /* which quadrant are we? */
# Line 204 | Line 228 | int    x, y;
228                          tp = tp->k[q].b;
229                          continue;
230                  }
231 <                if (tp->k[q].l != NULL)         /* good shot! */
232 <                        return(tp->k[q].l);
233 <                return(lp);                     /* else return what we have */
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 245 | 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;
256 <                        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();
270                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 >        }
331 >        return(li == lo);
332   }
333  
334  
335 < dev_value(c, p)                 /* add a pixel value to our output queue */
335 > dev_value(c, d, p)              /* add a pixel value to our quadtree */
336   COLR    c;
337 < FVECT   p;
337 > FVECT   d, p;
338   {
339 <        register RLEAF  *lp;
340 <
341 <        lp = newleaf();
342 <        VCOPY(lp->wp, p);
343 <        tmCvColrs(&lp->brt, lp->chr, c, 1);
344 <        addleaf(lp);
339 >        register int    li;
340 >        int     mapit;
341 >                                /* grab a leaf */
342 >        if (!imm_mode && falleaves >= 0) {      /* check for fallen leaves */
343 >                li = falleaves;
344 >                falleaves = qtL.wd[li];
345 >                mapit = qtL.tml <= qtL.tl ?
346 >                                (li < qtL.tml || li >= qtL.tl) :
347 >                                (li < qtL.tml && li >= qtL.tl) ;
348 >        } else {                                /* else allocate new one */
349 >                li = qtL.tl++;
350 >                if (qtL.tl >= qtL.nl)           /* next leaf in ring */
351 >                        qtL.tl = 0;
352 >                if (qtL.tl == qtL.bl)           /* need to shake some free */
353 >                        qtCompost(LFREEPCT);
354 >                mapit = 0;                      /* we'll map it later */
355 >        }
356 >        if (p == NULL)
357 >                VSUM(qtL.wp[li], odev.v.vp, d, FHUGE);
358 >        else
359 >                VCOPY(qtL.wp[li], p);
360 >        qtL.wd[li] = encodedir(d);
361 >        tmCvColrs(&qtL.brt[li], qtL.chr[li], (COLR *)c, 1);
362 >        if (putleaf(li, 1)) {
363 >                if (mapit)
364 >                        tmMapPixels((BYTE *)(qtL.rgb+li), qtL.brt+li,
365 >                                        (BYTE *)(qtL.chr+li), 1);
366 >                if (--rayqleft == 0)
367 >                        dev_flush();            /* flush output */
368 >        }
369   }
370  
371  
372   qtReplant()                     /* replant our tree using new view */
373   {
374          register int    i;
375 <
376 <        if (bleaf == tleaf)             /* anything to replant? */
375 >                                        /* anything to replant? */
376 >        if (qtL.bl == qtL.tl)
377                  return;
378 <        qtFreeTree(0);                  /* blow the tree away */
379 <                                        /* now rebuild it */
380 <        for (i = bleaf; i != tleaf; ) {
381 <                addleaf(leafpile+i);
382 <                if (++i >= nleaves) i = 0;
378 >        qtFreeTree(0);                  /* blow the old tree away */
379 >                                        /* regrow it in new place */
380 >        for (i = qtL.bl; i != qtL.tl; ) {
381 >                putleaf(i, 0);
382 >                if (++i >= qtL.nl) i = 0;
383          }
307        tmComputeMapping(0., 0., 0.);   /* update the display */
308        qtUpdate();
384   }
385  
386  
387 < static
388 < 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];
387 > qtMapLeaves(redo)               /* map our leaves to RGB */
388 > int     redo;
389   {
390 <        int     csm[3], nc;
391 <        BYTE    rgb[3];
392 <        int     quads = CH_ANY;
393 <        int     mx, my;
394 <        register int    i;
395 <                                        /* compute midpoint */
396 <        mx = (x0 + x1) >> 1;
397 <        my = (y0 + y1) >> 1;
398 <                                        /* see what to do */
399 <        if (l[0][0] >= mx)
400 <                quads &= ~(CHF(2)|CHF(0));
401 <        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;
390 >        int     aorg, alen, borg, blen;
391 >                                        /* recompute mapping? */
392 >        if (redo)
393 >                qtL.tml = qtL.bl;
394 >                                        /* already done? */
395 >        if (qtL.tml == qtL.tl)
396 >                return(1);
397 >                                        /* compute segments */
398 >        aorg = qtL.tml;
399 >        if (qtL.tl >= aorg) {
400 >                alen = qtL.tl - aorg;
401 >                blen = 0;
402          } else {
403 <                ca[0] = csm[0]; ca[1] = csm[1]; ca[2] = csm[2];
403 >                alen = qtL.nl - aorg;
404 >                borg = 0;
405 >                blen = qtL.tl;
406          }
407 <        if (!quads) return;
408 <                                        /* fill in gaps with average */
409 <        for (i = 0; i < 4; i++)
410 <                if (quads & CHF(i))
411 <                        dev_paintr(ca, i&01 ? mx : x0, i&02 ? my : y0,
412 <                                        i&01 ? x1 : mx, i&02 ? y1 : my);
413 < }
414 <
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];
407 >                                        /* (re)compute tone mapping? */
408 >        if (qtL.tml == qtL.bl) {
409 >                tmClearHisto();
410 >                tmAddHisto(qtL.brt+aorg, alen, 1);
411 >                if (blen > 0)
412 >                        tmAddHisto(qtL.brt+borg, blen, 1);
413 >                if (tmComputeMapping(0., 0., 0.) != TM_E_OK)
414 >                        return(0);
415          }
416 <                                        /* fill in gaps with average */
417 <        for (i = 0; gaps && i < 4; gaps >>= 1, i++)
418 <                if (gaps & 01)
419 <                        dev_paintr(ca, i&01 ? mx : x0, i&02 ? my : y0,
420 <                                        i&01 ? x1 : mx, i&02 ? y1 : my);
421 <        tp->flgs &= ~CH_ANY;            /* all done */
422 < }
423 <
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);
416 >        if (tmMapPixels((BYTE *)(qtL.rgb+aorg), qtL.brt+aorg,
417 >                        (BYTE *)(qtL.chr+aorg), alen) != TM_E_OK)
418 >                return(0);
419 >        if (blen > 0)
420 >                tmMapPixels((BYTE *)(qtL.rgb+borg), qtL.brt+borg,
421 >                                (BYTE *)(qtL.chr+borg), blen);
422 >        qtL.tml = qtL.tl;
423 >        return(1);
424   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines