Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,12 @@ public void Palettize(Span<TPixel> palette, ref short paletteIndex)
/// <returns>The palette index represented by the best matching leaf in the reduced tree.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int GetPaletteIndex(TPixel color)
=> this.Nodes[this.rootIndex].GetPaletteIndex(color.ToRgba32(), 0, this);
{
Rgba32 rgba = color.ToRgba32();
int luminance = ColorNumerics.Get8BitBT709Luminance(rgba.R, rgba.G, rgba.B);
ReadOnlySpan<Node> nodes = this.Nodes;
return nodes[this.rootIndex].GetPaletteIndex(rgba, luminance, 0, nodes);
}

/// <summary>
/// Records the most recently touched leaf so repeated colors can bypass another descent.
Expand Down Expand Up @@ -449,37 +454,46 @@ public void Initialize(int level, int colorBits, Hexadecatree tree, short index)
/// <param name="tree">The owning tree.</param>
public static void AddColor(int nodeIndex, Rgba32 color, int colorBits, int level, Hexadecatree tree)
{
ref Node node = ref tree.Nodes[nodeIndex];
if (node.Leaf)
{
Increment(nodeIndex, color, tree);
tree.TrackPrevious(nodeIndex);
return;
}

int index = GetColorIndex(color, level);
Span<short> children = node.Children;
short childIndex = children[index];
// The arena never grows and the brightness estimate does not depend on the level,
// so both are resolved once for the whole descent.
Span<Node> nodes = tree.Nodes;
int luminance = ColorNumerics.Get8BitBT709Luminance(color.R, color.G, color.B);

if (childIndex == -1)
// Keep descending until we reach the leaf bucket that should accumulate this sample.
while (true)
{
childIndex = tree.AllocateNode();
if (childIndex == -1)
ref Node node = ref nodes[nodeIndex];
if (node.Leaf)
{
// If the arena is exhausted and no node can be reclaimed yet, fall back to
// accumulating into the current node instead of failing the insert outright.
Increment(nodeIndex, color, tree);
tree.TrackPrevious(nodeIndex);
return;
}

ref Node child = ref tree.Nodes[childIndex];
child.Initialize(level + 1, colorBits, tree, childIndex);
children[index] = childIndex;
}
int index = GetColorIndex(color, luminance, level);
Span<short> children = node.Children;
short childIndex = children[index];

// Keep descending until we reach the leaf bucket that should accumulate this sample.
AddColor(childIndex, color, colorBits, level + 1, tree);
if (childIndex == -1)
{
childIndex = tree.AllocateNode();
if (childIndex == -1)
{
// If the arena is exhausted and no node can be reclaimed yet, fall back to
// accumulating into the current node instead of failing the insert outright.
Increment(nodeIndex, color, tree);
tree.TrackPrevious(nodeIndex);
return;
}

ref Node child = ref nodes[childIndex];
child.Initialize(level + 1, colorBits, tree, childIndex);
children[index] = childIndex;
}

nodeIndex = childIndex;
level++;
}
}

/// <summary>
Expand Down Expand Up @@ -598,22 +612,23 @@ public void ConstructPalette(Hexadecatree tree, Span<TPixel> palette, ref short
/// Resolves the palette index represented by this node for the supplied color.
/// </summary>
/// <param name="color">The color to resolve.</param>
/// <param name="luminance">The BT.709 luminance of <paramref name="color"/>.</param>
/// <param name="level">The current tree depth.</param>
/// <param name="tree">The owning tree.</param>
/// <param name="nodes">The node arena of the owning tree.</param>
/// <returns>The palette index for the best reachable leaf, or <c>-1</c> if no leaf can be reached.</returns>
public int GetPaletteIndex(Rgba32 color, int level, Hexadecatree tree)
public readonly int GetPaletteIndex(Rgba32 color, int luminance, int level, ReadOnlySpan<Node> nodes)
{
if (this.Leaf)
{
return this.PaletteIndex;
}

int colorIndex = GetColorIndex(color, level);
Span<short> children = this.Children;
int colorIndex = GetColorIndex(color, luminance, level);
ReadOnlySpan<short> children = this.children;
int childIndex = children[colorIndex];
if (childIndex != -1)
{
return tree.Nodes[childIndex].GetPaletteIndex(color, level + 1, tree);
return nodes[childIndex].GetPaletteIndex(color, luminance, level + 1, nodes);
}

// After reductions the exact branch can disappear, so fall back to the first reachable descendant leaf.
Expand All @@ -622,7 +637,7 @@ public int GetPaletteIndex(Rgba32 color, int level, Hexadecatree tree)
childIndex = children[i];
if (childIndex != -1)
{
int childPaletteIndex = tree.Nodes[childIndex].GetPaletteIndex(color, level + 1, tree);
int childPaletteIndex = nodes[childIndex].GetPaletteIndex(color, luminance, level + 1, nodes);
if (childPaletteIndex != -1)
{
return childPaletteIndex;
Expand All @@ -637,23 +652,22 @@ public int GetPaletteIndex(Rgba32 color, int level, Hexadecatree tree)
/// Computes the child slot for a color at the supplied tree level.
/// </summary>
/// <param name="color">The color being routed.</param>
/// <param name="luminance">The BT.709 luminance of <paramref name="color"/>.</param>
/// <param name="level">The tree depth whose bit plane should be sampled.</param>
/// <returns>The child slot index for the color at the supplied level.</returns>
/// <remarks>
/// For fully opaque mid-tone colors the tree ignores alpha and routes on RGB only, preserving more branch
/// resolution for visible color detail. For transparent, dark, and light colors it includes alpha as the
/// most significant routing bit so opacity changes can form their own branches.
/// </remarks>
public static int GetColorIndex(Rgba32 color, int level)
public static int GetColorIndex(Rgba32 color, int luminance, int level)
{
// Sample one bit plane per level, starting at the most significant bit and moving downward.
int shift = 7 - level;
byte mask = (byte)(1 << shift);

// Use BT.709 luminance as a cheap brightness estimate for deciding whether alpha carries
// The BT.709 luminance is a cheap brightness estimate for deciding whether alpha carries
// useful information at this level for fully opaque colors.
int luminance = ColorNumerics.Get8BitBT709Luminance(color.R, color.G, color.B);

// Scale the brightness thresholds with depth so deeper levels become stricter about when
// to spend a branch bit on alpha instead of RGB detail.
int darkThreshold = 128 >> level;
Expand Down