/// <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> /// <param name="buffer">The buffer array.</param> public void AddColor(TColor pixel, int colorBits, int level, Octree octree, byte[] buffer) { // Update the color information if this is a leaf if (this.leaf) { this.Increment(pixel, buffer); // Setup the previous node octree.TrackPrevious(this); } else { // Go to the next level down in the tree int shift = 7 - level; pixel.ToXyzwBytes(buffer, 0); int index = ((buffer[3] & Mask[0]) >> (shift - 3)) | ((buffer[2] & Mask[level + 1]) >> (shift - 2)) | ((buffer[1] & Mask[level + 1]) >> (shift - 1)) | ((buffer[0] & Mask[level + 1]) >> shift); OctreeNode child = this.children[index]; if (child == null) { // Create a new child node and store it in the array child = new OctreeNode(level + 1, colorBits, octree); this.children[index] = child; } // Add the color to the child node child.AddColor(pixel, colorBits, level + 1, octree, buffer); } }
/// <summary> /// Return the palette index for the passed color /// </summary> /// <param name="pixel">The pixel data.</param> /// <param name="level">The level.</param> /// <param name="buffer">The buffer array.</param> /// <returns> /// The <see cref="int"/> representing the index of the pixel in the palette. /// </returns> public int GetPaletteIndex(TColor pixel, int level, byte[] buffer) { int index = this.paletteIndex; if (!this.leaf) { int shift = 7 - level; pixel.ToXyzwBytes(buffer, 0); int pixelIndex = ((buffer[3] & Mask[0]) >> (shift - 3)) | ((buffer[2] & Mask[level + 1]) >> (shift - 2)) | ((buffer[1] & Mask[level + 1]) >> (shift - 1)) | ((buffer[0] & Mask[level + 1]) >> shift); if (this.children[pixelIndex] != null) { index = this.children[pixelIndex].GetPaletteIndex(pixel, level + 1, buffer); } else { throw new Exception($"Cannot retrive a pixel at the given index {pixelIndex}."); } } return(index); }
/// <summary> /// Increment the pixel count and add to the color information /// </summary> /// <param name="pixel">The pixel to add.</param> /// <param name="buffer">The buffer array.</param> public void Increment(TColor pixel, byte[] buffer) { pixel.ToXyzwBytes(buffer, 0); this.pixelCount++; this.red += buffer[0]; this.green += buffer[1]; this.blue += buffer[2]; }