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

Comparing ray/src/rt/colortab.c (file contents):
Revision 1.2 by greg, Mon Oct 2 17:12:42 1989 UTC vs.
Revision 1.3 by greg, Tue Oct 3 11:10:10 1989 UTC

# Line 20 | Line 20 | static char SCCSid[] = "$SunId$ LBL";
20  
21   #define NULL            0
22                                  /* histogram resolution */
23 < #define NRED            28
23 > #define NRED            24
24   #define NGRN            32
25 < #define NBLU            22
25 > #define NBLU            16
26   #define HMAX            NGRN
27                                  /* minimum box count for adaptive partition */
28   #define MINSAMP         7
29 +                                /* maximum distance^2 before color reassign */
30 + #define MAXDST2         5
31                                  /* maximum frame buffer depth */
32   #define FBDEPTH         8
33                                  /* color map resolution */
34 < #define MAPSIZ          128
34 > #define MAPSIZ          256
35                                  /* map a color */
36 < #define map_col(c,p)    clrmap[p][ colval(c,p)<1. ? \
36 > #define map_col(c,p)    clrmap[ colval(c,p)<1. ? \
37                                  (int)(colval(c,p)*MAPSIZ) : MAPSIZ-1 ]
38                                  /* color partition tree */
39   #define CNODE           short
# Line 42 | Line 44 | static char SCCSid[] = "$SunId$ LBL";
44   #define prim(cn)        ((cn)&3)
45   #define pval(cn)        ((cn)>>2)
46                                  /* our color table */
47 < COLR    clrtab[1<<FBDEPTH];
47 > struct tabent {
48 >        long    sum[3];         /* sum of colors using this entry */
49 >        long    n;              /* number of colors */
50 >        short   ent[3];         /* current table value */
51 > }       clrtab[1<<FBDEPTH];
52                                  /* our color correction map */
53 < static BYTE     clrmap[3][MAPSIZ];
53 > static BYTE     clrmap[MAPSIZ];
54                                  /* histogram of colors used */
55   static unsigned histo[NRED][NGRN][NBLU];
56                                  /* initial color cube boundaries */
57   static int      CLRCUBE[3][2] = {0,NRED,0,NGRN,0,NBLU};
58                                  /* color cube partition */
59   static CNODE    ctree[1<<(FBDEPTH+1)];
60 +                                /* callback for pixel assignment */
61 + static int      (*set_pixel)();
62  
63  
64 < COLR *
65 < get_ctab(ncolors)       /* assign a color table with max ncolors */
64 > int
65 > new_ctab(ncolors, cset)         /* start new color table with max ncolors */
66   int     ncolors;
67 + int     (*cset)();
68   {
69 <        if (ncolors < 1 || ncolors > 1<<FBDEPTH)
70 <                return(NULL);
71 <                                /* partition color table */
69 >        if (ncolors < 1 || ncolors > 1<<FBDEPTH || cset == NULL)
70 >                return(0);
71 >                                /* assign pixel callback routine */
72 >        set_pixel = cset;
73 >                                /* clear color table */
74 >        bzero(clrtab, sizeof(clrtab));
75 >                                /* partition color space */
76          cut(ctree, FBDEPTH, CLRCUBE, 0, ncolors);
77                                  /* clear histogram */
78          bzero(histo, sizeof(histo));
79 <                                /* return color table */
80 <        return(clrtab);
79 >                                /* return number of colors used */
80 >        return(ncolors);
81   }
82  
83  
# Line 72 | Line 85 | int
85   get_pixel(col)                  /* get pixel for color */
86   COLOR   col;
87   {
88 +        int     r, g, b;
89          int     cv[3];
90 <        register CNODE  *tp;
90 >        register union { CNODE *t; struct tabent *e; }  p;
91          register int    h;
92 <                                        /* map color */
93 <        cv[RED] = map_col(col,RED);
94 <        cv[GRN] = map_col(col,GRN);
95 <        cv[BLU] = map_col(col,BLU);
96 <                                        /* add to histogram */
92 >                                                /* map color */
93 >        r = map_col(col,RED);
94 >        g = map_col(col,GRN);
95 >        b = map_col(col,BLU);
96 >                                                /* reduce resolution */
97 >        cv[RED] = (r*NRED)>>8;
98 >        cv[GRN] = (g*NGRN)>>8;
99 >        cv[BLU] = (b*NBLU)>>8;
100 >                                                /* add to histogram */
101          histo[cv[RED]][cv[GRN]][cv[BLU]]++;
102 <                                        /* find pixel value in tree */
103 <        tp = ctree;
102 >                                                /* find pixel in tree */
103 >        p.t = ctree;
104          for (h = FBDEPTH; h > 0; h--) {
105 <                if (is_pval(*tp))
105 >                if (is_pval(*p.t))
106                          break;
107 <                if (cv[prim(*tp)] < part(*tp))
108 <                        tp++;                   /* left branch */
107 >                if (cv[prim(*p.t)] < part(*p.t))
108 >                        p.t++;          /* left branch */
109                  else
110 <                        tp += 1<<h;             /* right branch */
110 >                        p.t += 1<<h;    /* right branch */
111          }
112 +        h = pval(*p.t);
113 +                                                /* add to color table */
114 +        p.e = clrtab + h;
115 +                                        /* add to sum */
116 +        p.e->sum[RED] += r;
117 +        p.e->sum[GRN] += g;
118 +        p.e->sum[BLU] += b;
119 +        p.e->n++;
120 +                                        /* recompute average */
121 +        r = p.e->sum[RED] / p.e->n;
122 +        g = p.e->sum[GRN] / p.e->n;
123 +        b = p.e->sum[BLU] / p.e->n;
124 +                                        /* check for movement */
125 +        if (p.e->n == 1 ||
126 +                        (r-p.e->ent[RED])*(r-p.e->ent[RED]) +
127 +                        (g-p.e->ent[GRN])*(g-p.e->ent[GRN]) +
128 +                        (b-p.e->ent[BLU])*(b-p.e->ent[BLU]) > MAXDST2) {
129 +                p.e->ent[RED] = r;
130 +                p.e->ent[GRN] = g;      /* reassign pixel */
131 +                p.e->ent[BLU] = b;
132   #ifdef notdef
133 <        printf("distance (%d,%d,%d)\n",
134 <                cv[RED] - clrtab[pval(*tp)][RED]*NRED/256,
97 <                cv[GRN] - clrtab[pval(*tp)][GRN]*NGRN/256,
98 <                cv[BLU] - clrtab[pval(*tp)][BLU]*NBLU/256);
133 >                printf("pixel %d = (%d,%d,%d) (%d refs)\n",
134 >                                h, r, g, b, p.e->n);
135   #endif
136 <        return(pval(*tp));
136 >                (*set_pixel)(h, r, g, b);
137 >        }
138 >        return(h);                              /* return pixel value */
139   }
140  
141  
# Line 105 | Line 143 | make_cmap(gam)                 /* make gamma correction map */
143   double  gam;
144   {
145          extern double   pow();
108        double  d;
146          register int    i;
147          
148 <        for (i = 0; i < MAPSIZ; i++) {
149 <                d = pow(i/(double)MAPSIZ, 1.0/gam);
113 <                clrmap[RED][i] = d * NRED;
114 <                clrmap[GRN][i] = d * NGRN;
115 <                clrmap[BLU][i] = d * NBLU;
116 <        }
148 >        for (i = 0; i < MAPSIZ; i++)
149 >                clrmap[i] = 256.0 * pow(i/(double)MAPSIZ, 1.0/gam);
150   }
151  
152  
# Line 126 | Line 159 | int    c0, c1;
159   {
160          int     kb[3][2];
161          
162 <        if (c1-c0 == 1) {               /* assign color */
130 <                clrtab[c0][RED] = ((box[RED][0]+box[RED][1])<<7)/NRED;
131 <                clrtab[c0][GRN] = ((box[GRN][0]+box[GRN][1])<<7)/NGRN;
132 <                clrtab[c0][BLU] = ((box[BLU][0]+box[BLU][1])<<7)/NBLU;
133 <                clrtab[c0][EXP] = COLXS;
162 >        if (c1-c0 <= 1) {               /* assign pixel */
163                  *tree = set_pval(c0);
135 #ifdef notdef
136                printf("final box size = (%d,%d,%d)\n",
137                        box[RED][1] - box[RED][0],
138                        box[GRN][1] - box[GRN][0],
139                        box[BLU][1] - box[BLU][0]);
140 #endif
164                  return;
165          }
166                                          /* split box */
167          *tree = split(box);
168          bcopy(box, kb, sizeof(kb));
169 +                                                /* do left (lesser) branch */
170          kb[prim(*tree)][1] = part(*tree);
171 <        cut(tree+1, height-1, kb, c0, (c0+c1)>>1);              /* lesser */
171 >        cut(tree+1, height-1, kb, c0, (c0+c1)>>1);
172 >                                                /* do right branch */
173          kb[prim(*tree)][0] = part(*tree);
174          kb[prim(*tree)][1] = box[prim(*tree)][1];
175 <        cut(tree+(1<<height), height-1, kb, (c0+c1)>>1, c1);    /* greater */
175 >        cut(tree+(1<<height), height-1, kb, (c0+c1)>>1, c1);
176   }
177  
178  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines