/// <summary> /// Add a color into the tree /// </summary> /// <param name="pixel">The color</param> /// <param name="colorBits">The number of significant color bits</param> /// <param name="level">The level in the tree</param> /// <param name="octree">The tree to which this node belongs</param> public void AddColor ( Color32* pixel , int colorBits , int level , Octree octree ) { // Update the color information if this is a leaf if ( _leaf ) { Increment ( pixel ) ; // Setup the previous node octree.TrackPrevious ( this ) ; } else { // Go to the next level down in the tree int shift = 7 - level ; int index = ( ( pixel->Red & mask[level] ) >> ( shift - 2 ) ) | ( ( pixel->Green & mask[level] ) >> ( shift - 1 ) ) | ( ( pixel->Blue & mask[level] ) >> ( shift ) ) ; OctreeNode child = _children[index] ; if ( null == child ) { // Create a new child node & store in the array child = new OctreeNode ( level + 1 , colorBits , octree ) ; _children[index] = child ; } // Add the color to the child node child.AddColor ( pixel , colorBits , level + 1 , octree ) ; } }
/// <summary> /// Keep track of the previous node that was quantized /// </summary> /// <param name="node">The node last quantized</param> public void TrackPrevious ( OctreeNode node ) { _previousNode = node ; }
/// <summary> /// Construct the node /// </summary> /// <param name="level">The level in the tree = 0 - 7</param> /// <param name="colorBits">The number of significant color bits in the image</param> /// <param name="octree">The tree to which this node belongs</param> public OctreeNode ( int level , int colorBits , Octree octree ) { // Construct the new node _leaf = ( level == colorBits ) ; _red = _green = _blue = 0 ; _pixelCount = 0 ; // If a leaf, increment the leaf count if ( _leaf ) { octree.Leaves++ ; _nextReducible = null ; _children = null ; } else { // Otherwise add this to the reducible nodes _nextReducible = octree.ReducibleNodes[level] ; octree.ReducibleNodes[level] = this ; _children = new OctreeNode[8] ; } }
/// <summary> /// Construct the octree /// </summary> /// <param name="maxColorBits">The maximum number of significant bits in the image</param> public Octree ( int maxColorBits ) { _maxColorBits = maxColorBits ; _leafCount = 0 ; _reducibleNodes = new OctreeNode[9] ; _root = new OctreeNode ( 0 , _maxColorBits , this ) ; _previousColor = 0 ; _previousNode = null ; }