/// <summary> /// Increment the pixel count and add to the color information /// </summary> public void Increment ( Color32* pixel ) { _pixelCount++ ; _red += pixel->Red ; _green += pixel->Green ; _blue += pixel->Blue ; }
/// <summary> /// Return the palette index for the passed color /// </summary> public int GetPaletteIndex ( Color32* pixel , int level ) { int paletteIndex = _paletteIndex ; if ( !_leaf ) { int shift = 7 - level ; int index = ( ( pixel->Red & mask[level] ) >> ( shift - 2 ) ) | ( ( pixel->Green & mask[level] ) >> ( shift - 1 ) ) | ( ( pixel->Blue & mask[level] ) >> ( shift ) ) ; if ( null != _children[index] ) paletteIndex = _children[index].GetPaletteIndex ( pixel , level + 1 ) ; else throw new Exception ( "Didn't expect this!" ) ; } return paletteIndex ; }
/// <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> /// Get the palette index for the passed color /// </summary> /// <param name="pixel"></param> /// <returns></returns> public int GetPaletteIndex ( Color32* pixel ) { return _root.GetPaletteIndex ( pixel , 0 ) ; }
/// <summary> /// Add a given color value to the octree /// </summary> /// <param name="pixel"></param> public void AddColor ( Color32* pixel ) { // Check if this request is for the same color as the last if ( _previousColor == pixel->ARGB ) { // If so, check if I have a previous node setup. This will only ocurr if the first color in the image // happens to be black, with an alpha component of zero. if ( null == _previousNode ) { _previousColor = pixel->ARGB ; _root.AddColor ( pixel , _maxColorBits , 0 , this ) ; } else // Just update the previous node _previousNode.Increment ( pixel ) ; } else { _previousColor = pixel->ARGB ; _root.AddColor ( pixel , _maxColorBits , 0 , this ) ; } }
private byte QuantizePixel ( Color32* pixel, Octree octree ) { byte paletteIndex = (byte)MaxColors ; // The color at [_maxColors] is set to transparent // Get the palette index if this non-transparent if ( pixel->Alpha > 0 ) paletteIndex = (byte)octree.GetPaletteIndex ( pixel ) ; return paletteIndex ; }