Skip to content

Commit

Permalink
if transparencyIndex is outside the palette, ignore it
Browse files Browse the repository at this point in the history
  • Loading branch information
antonfirsov committed Sep 1, 2023
1 parent 1a6b465 commit b48dbc5
Showing 1 changed file with 8 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,16 @@ public EuclideanPixelMap(Configuration configuration, ReadOnlyMemory<TPixel> pal
this.rgbaPalette = new Rgba32[palette.Length];
this.cache = new ColorDistanceCache(configuration.MemoryAllocator);
PixelOperations<TPixel>.Instance.ToRgba32(configuration, this.Palette.Span, this.rgbaPalette);
this.transparentIndex = transparentIndex;

// If the provided transparentIndex is outside of the palette, silently ignore it.
this.transparentIndex = transparentIndex < this.Palette.Length ? transparentIndex : -1;
}

/// <summary>
/// Gets the color palette of this <see cref="EuclideanPixelMap{TPixel}"/>.
/// The palette memory is owned by the palette source that created it.
/// </summary>
public ReadOnlyMemory<TPixel> Palette
{
[MethodImpl(InliningOptions.ShortMethod)]
get;

[MethodImpl(InliningOptions.ShortMethod)]
private set;
}
public ReadOnlyMemory<TPixel> Palette { get; private set; }

/// <summary>
/// Returns the closest color in the palette and the index of that pixel.
Expand Down Expand Up @@ -106,10 +101,10 @@ public void Clear(ReadOnlyMemory<TPixel> palette)
}

/// <summary>
/// Allows setting the transparent index after construction.
/// Allows setting the transparent index after construction. If the provided transparentIndex is outside of the palette, silently ignore it.
/// </summary>
/// <param name="index">An explicit index at which to match transparent pixels.</param>
public void SetTransparentIndex(int index) => this.transparentIndex = index;
public void SetTransparentIndex(int index) => this.transparentIndex = index < this.Palette.Length ? index : -1;

[MethodImpl(InliningOptions.ShortMethod)]
private int GetClosestColorSlow(Rgba32 rgba, ref TPixel paletteRef, out TPixel match)
Expand All @@ -122,19 +117,9 @@ private int GetClosestColorSlow(Rgba32 rgba, ref TPixel paletteRef, out TPixel m
{
// We have explicit instructions. No need to search.
index = this.transparentIndex;
DebugGuard.MustBeLessThan(index, this.Palette.Length, nameof(index));
this.cache.Add(rgba, (byte)index);

if (index >= 0 && index < this.Palette.Length)
{
match = Unsafe.Add(ref paletteRef, (uint)index);
}
else
{
Unsafe.SkipInit(out TPixel pixel);
pixel.FromScaledVector4(Vector4.Zero);
match = pixel;
}

match = Unsafe.Add(ref paletteRef, (uint)index);
return index;
}

Expand Down

0 comments on commit b48dbc5

Please sign in to comment.