From 132bf7f54a79877da345227ef8b88ddf576e7d5a Mon Sep 17 00:00:00 2001 From: Darth Affe Date: Mon, 8 Jul 2024 23:55:48 +0200 Subject: [PATCH] Changed all public PixelHelper-methods to be extensions; removed unused sorts --- HPPH.Test/AverageTests.cs | 4 +- HPPH.Test/ConvertTests.cs | 18 +- HPPH.Test/MinMaxTests.cs | 4 +- HPPH.Test/QuantizeTests.cs | 12 +- HPPH.Test/SortTests.cs | 14 +- HPPH.Test/SumTests.cs | 4 +- .../PixelHelper.g.cs | 253 +----------------- HPPH/PixelHelper.Average.cs | 6 +- HPPH/PixelHelper.Convert.cs | 16 +- HPPH/PixelHelper.MinMax.cs | 6 +- HPPH/PixelHelper.Quantize.cs | 8 +- HPPH/PixelHelper.Sort.cs | 29 +- HPPH/PixelHelper.Sum.cs | 6 +- 13 files changed, 53 insertions(+), 327 deletions(-) diff --git a/HPPH.Test/AverageTests.cs b/HPPH.Test/AverageTests.cs index fbc375a..aa2434a 100644 --- a/HPPH.Test/AverageTests.cs +++ b/HPPH.Test/AverageTests.cs @@ -26,7 +26,7 @@ public class AverageTests ReadOnlySpan span = data; ColorRGB reference = ReferencePixelHelper.Average(span); - ColorRGB test = PixelHelper.Average(span); + ColorRGB test = span.Average(); Assert.AreEqual(reference.R, test.R, "R differs"); Assert.AreEqual(reference.G, test.G, "G differs"); @@ -54,7 +54,7 @@ public class AverageTests ReadOnlySpan span = data; ColorRGBA reference = ReferencePixelHelper.Average(span); - ColorRGBA test = PixelHelper.Average(span); + ColorRGBA test = span.Average(); Assert.AreEqual(reference.R, test.R, "R differs"); Assert.AreEqual(reference.G, test.G, "G differs"); diff --git a/HPPH.Test/ConvertTests.cs b/HPPH.Test/ConvertTests.cs index 1dfd759..44266dd 100644 --- a/HPPH.Test/ConvertTests.cs +++ b/HPPH.Test/ConvertTests.cs @@ -18,7 +18,7 @@ public class ConvertTests Span sourceData = new ColorRGB[referenceData.Length]; referenceData.CopyTo(sourceData); - Span result = PixelHelper.Convert(sourceData); + Span result = sourceData.Convert(); Assert.AreEqual(referenceData.Length, result.Length); for (int i = 0; i < referenceData.Length; i++) @@ -48,7 +48,7 @@ public class ConvertTests Span sourceData = new ColorRGBA[referenceData.Length]; referenceData.CopyTo(sourceData); - Span result = PixelHelper.Convert(sourceData); + Span result = sourceData.Convert(); Assert.AreEqual(referenceData.Length, result.Length); for (int i = 0; i < referenceData.Length; i++) @@ -78,7 +78,7 @@ public class ConvertTests Span sourceData = new ColorRGBA[referenceData.Length]; referenceData.CopyTo(sourceData); - Span result = PixelHelper.Convert(sourceData); + Span result = sourceData.Convert(); Assert.AreEqual(referenceData.Length, result.Length); for (int i = 0; i < referenceData.Length; i++) @@ -108,7 +108,7 @@ public class ConvertTests Span sourceData = new ColorRGBA[referenceData.Length]; referenceData.CopyTo(sourceData); - Span result = PixelHelper.Convert(sourceData); + Span result = sourceData.Convert(); Assert.AreEqual(referenceData.Length, result.Length); for (int i = 0; i < referenceData.Length; i++) @@ -138,7 +138,7 @@ public class ConvertTests Span sourceData = new ColorRGBA[referenceData.Length]; referenceData.CopyTo(sourceData); - Span result = PixelHelper.Convert(sourceData); + Span result = sourceData.Convert(); Assert.AreEqual(referenceData.Length, result.Length); for (int i = 0; i < referenceData.Length; i++) @@ -168,7 +168,7 @@ public class ConvertTests Span sourceData = new ColorRGB[referenceData.Length]; referenceData.CopyTo(sourceData); - Span result = PixelHelper.Convert(sourceData); + Span result = sourceData.Convert(); Assert.AreEqual(referenceData.Length, result.Length); for (int i = 0; i < referenceData.Length; i++) @@ -198,7 +198,7 @@ public class ConvertTests Span sourceData = new ColorRGB[referenceData.Length]; referenceData.CopyTo(sourceData); - Span result = PixelHelper.Convert(sourceData); + Span result = sourceData.Convert(); Assert.AreEqual(referenceData.Length, result.Length); for (int i = 0; i < referenceData.Length; i++) @@ -228,7 +228,7 @@ public class ConvertTests Span sourceData = new ColorRGB[referenceData.Length]; referenceData.CopyTo(sourceData); - Span result = PixelHelper.Convert(sourceData); + Span result = sourceData.Convert(); Assert.AreEqual(referenceData.Length, result.Length); for (int i = 0; i < referenceData.Length; i++) @@ -258,7 +258,7 @@ public class ConvertTests Span sourceData = new ColorRGB[referenceData.Length]; referenceData.CopyTo(sourceData); - Span result = PixelHelper.Convert(sourceData); + Span result = sourceData.Convert(); Assert.AreEqual(referenceData.Length, result.Length); for (int i = 0; i < referenceData.Length; i++) diff --git a/HPPH.Test/MinMaxTests.cs b/HPPH.Test/MinMaxTests.cs index 5f3ecb8..3484994 100644 --- a/HPPH.Test/MinMaxTests.cs +++ b/HPPH.Test/MinMaxTests.cs @@ -26,7 +26,7 @@ public class MinMaxTests ReadOnlySpan span = data; IMinMax reference = ReferencePixelHelper.MinMax(span); - IMinMax test = PixelHelper.MinMax(span); + IMinMax test = span.MinMax(); Assert.AreEqual(reference.RedMin, test.RedMin, "RedMin differs"); Assert.AreEqual(reference.GreenMin, test.GreenMin, "GreenMin differs"); @@ -64,7 +64,7 @@ public class MinMaxTests ReadOnlySpan span = data; IMinMax reference = ReferencePixelHelper.MinMax(span); - IMinMax test = PixelHelper.MinMax(span); + IMinMax test = span.MinMax(); Assert.AreEqual(reference.RedMin, test.RedMin, "RedMin differs"); Assert.AreEqual(reference.GreenMin, test.GreenMin, "GreenMin differs"); diff --git a/HPPH.Test/QuantizeTests.cs b/HPPH.Test/QuantizeTests.cs index 44bda39..ef1e908 100644 --- a/HPPH.Test/QuantizeTests.cs +++ b/HPPH.Test/QuantizeTests.cs @@ -16,7 +16,7 @@ public class CreateColorPaletteTests Span span = data; ColorRGB[] reference = [.. ReferencePixelHelper.CreateColorPalette(span, 1).OrderBy(x => x.R).ThenBy(x => x.G).ThenBy(x => x.B).ThenBy(x => x.A)]; - ColorRGB[] test = [.. PixelHelper.CreateColorPalette(span, 1).OrderBy(x => x.R).ThenBy(x => x.G).ThenBy(x => x.B).ThenBy(x => x.A)]; + ColorRGB[] test = [.. span.CreateColorPalette(1).OrderBy(x => x.R).ThenBy(x => x.G).ThenBy(x => x.B).ThenBy(x => x.A)]; Assert.AreEqual(reference.Length, test.Length, "Palette Size differs"); @@ -34,7 +34,7 @@ public class CreateColorPaletteTests Span span = data; ColorRGB[] reference = [.. ReferencePixelHelper.CreateColorPalette(span, 2).OrderBy(x => x.R).ThenBy(x => x.G).ThenBy(x => x.B).ThenBy(x => x.A)]; - ColorRGB[] test = [.. PixelHelper.CreateColorPalette(span, 2).OrderBy(x => x.R).ThenBy(x => x.G).ThenBy(x => x.B).ThenBy(x => x.A)]; + ColorRGB[] test = [.. span.CreateColorPalette(2).OrderBy(x => x.R).ThenBy(x => x.G).ThenBy(x => x.B).ThenBy(x => x.A)]; Assert.AreEqual(reference.Length, test.Length, "Palette Size differs"); @@ -52,7 +52,7 @@ public class CreateColorPaletteTests Span span = data; ColorRGB[] reference = [.. ReferencePixelHelper.CreateColorPalette(span, 16).OrderBy(x => x.R).ThenBy(x => x.G).ThenBy(x => x.B).ThenBy(x => x.A)]; - ColorRGB[] test = [.. PixelHelper.CreateColorPalette(span, 16).OrderBy(x => x.R).ThenBy(x => x.G).ThenBy(x => x.B).ThenBy(x => x.A)]; + ColorRGB[] test = [.. span.CreateColorPalette(16).OrderBy(x => x.R).ThenBy(x => x.G).ThenBy(x => x.B).ThenBy(x => x.A)]; Assert.AreEqual(reference.Length, test.Length, "Palette Size differs"); @@ -70,7 +70,7 @@ public class CreateColorPaletteTests Span span = data; ColorRGBA[] reference = [.. ReferencePixelHelper.CreateColorPalette(span, 1).OrderBy(x => x.R).ThenBy(x => x.G).ThenBy(x => x.B).ThenBy(x => x.A)]; - ColorRGBA[] test = [.. PixelHelper.CreateColorPalette(span, 1).OrderBy(x => x.R).ThenBy(x => x.G).ThenBy(x => x.B).ThenBy(x => x.A)]; + ColorRGBA[] test = [.. span.CreateColorPalette(1).OrderBy(x => x.R).ThenBy(x => x.G).ThenBy(x => x.B).ThenBy(x => x.A)]; Assert.AreEqual(reference.Length, test.Length, "Palette Size differs"); @@ -88,7 +88,7 @@ public class CreateColorPaletteTests Span span = data; ColorRGBA[] reference = [.. ReferencePixelHelper.CreateColorPalette(span, 2).OrderBy(x => x.R).ThenBy(x => x.G).ThenBy(x => x.B).ThenBy(x => x.A)]; - ColorRGBA[] test = [.. PixelHelper.CreateColorPalette(span, 2).OrderBy(x => x.R).ThenBy(x => x.G).ThenBy(x => x.B).ThenBy(x => x.A)]; + ColorRGBA[] test = [.. span.CreateColorPalette(2).OrderBy(x => x.R).ThenBy(x => x.G).ThenBy(x => x.B).ThenBy(x => x.A)]; Assert.AreEqual(reference.Length, test.Length, "Palette Size differs"); @@ -106,7 +106,7 @@ public class CreateColorPaletteTests Span span = data; ColorRGBA[] reference = [.. ReferencePixelHelper.CreateColorPalette(span, 16).OrderBy(x => x.R).ThenBy(x => x.G).ThenBy(x => x.B).ThenBy(x => x.A)]; - ColorRGBA[] test = [.. PixelHelper.CreateColorPalette(span, 16).OrderBy(x => x.R).ThenBy(x => x.G).ThenBy(x => x.B).ThenBy(x => x.A)]; + ColorRGBA[] test = [.. span.CreateColorPalette(16).OrderBy(x => x.R).ThenBy(x => x.G).ThenBy(x => x.B).ThenBy(x => x.A)]; Assert.AreEqual(reference.Length, test.Length, "Palette Size differs"); diff --git a/HPPH.Test/SortTests.cs b/HPPH.Test/SortTests.cs index e41159a..cc3c667 100644 --- a/HPPH.Test/SortTests.cs +++ b/HPPH.Test/SortTests.cs @@ -20,7 +20,7 @@ public class SortTests referenceSpan.CopyTo(testSpan); ReferencePixelHelper.SortByRed(referenceSpan); - PixelHelper.SortByRed(testSpan); + testSpan.SortByRed(); for (int i = 0; i < referenceData.Length; i++) Assert.AreEqual(referenceSpan[i], testSpan[i], $"Index {i} differs"); @@ -39,7 +39,7 @@ public class SortTests referenceSpan.CopyTo(testSpan); ReferencePixelHelper.SortByGreen(referenceSpan); - PixelHelper.SortByGreen(testSpan); + testSpan.SortByGreen(); for (int i = 0; i < referenceData.Length; i++) Assert.AreEqual(referenceSpan[i], testSpan[i], $"Index {i} differs"); @@ -58,7 +58,7 @@ public class SortTests referenceSpan.CopyTo(testSpan); ReferencePixelHelper.SortByBlue(referenceSpan); - PixelHelper.SortByBlue(testSpan); + testSpan.SortByBlue(); for (int i = 0; i < referenceData.Length; i++) Assert.AreEqual(referenceSpan[i], testSpan[i], $"Index {i} differs"); @@ -77,7 +77,7 @@ public class SortTests referenceSpan.CopyTo(testSpan); ReferencePixelHelper.SortByRed(referenceSpan); - PixelHelper.SortByRed(testSpan); + testSpan.SortByRed(); for (int i = 0; i < referenceData.Length; i++) Assert.AreEqual(referenceSpan[i], testSpan[i], $"Index {i} differs"); @@ -96,7 +96,7 @@ public class SortTests referenceSpan.CopyTo(testSpan); ReferencePixelHelper.SortByGreen(referenceSpan); - PixelHelper.SortByGreen(testSpan); + testSpan.SortByGreen(); for (int i = 0; i < referenceData.Length; i++) Assert.AreEqual(referenceSpan[i], testSpan[i], $"Index {i} differs"); @@ -115,7 +115,7 @@ public class SortTests referenceSpan.CopyTo(testSpan); ReferencePixelHelper.SortByBlue(referenceSpan); - PixelHelper.SortByBlue(testSpan); + testSpan.SortByBlue(); for (int i = 0; i < referenceData.Length; i++) Assert.AreEqual(referenceSpan[i], testSpan[i], $"Index {i} differs"); @@ -134,7 +134,7 @@ public class SortTests referenceSpan.CopyTo(testSpan); ReferencePixelHelper.SortByAlpha(referenceSpan); - PixelHelper.SortByAlpha(testSpan); + testSpan.SortByAlpha(); for (int i = 0; i < referenceData.Length; i++) Assert.AreEqual(referenceSpan[i], testSpan[i], $"Index {i} differs"); diff --git a/HPPH.Test/SumTests.cs b/HPPH.Test/SumTests.cs index d227e5e..a3a69a2 100644 --- a/HPPH.Test/SumTests.cs +++ b/HPPH.Test/SumTests.cs @@ -26,7 +26,7 @@ public class SumTests ReadOnlySpan span = data; ISum reference = ReferencePixelHelper.Sum(span); - ISum test = PixelHelper.Sum(span); + ISum test = span.Sum(); Assert.AreEqual(reference.R, test.R, "R differs"); Assert.AreEqual(reference.G, test.G, "G differs"); @@ -54,7 +54,7 @@ public class SumTests ReadOnlySpan span = data; ISum reference = ReferencePixelHelper.Sum(span); - ISum test = PixelHelper.Sum(span); + ISum test = span.Sum(); Assert.AreEqual(reference.R, test.R, "R differs"); Assert.AreEqual(reference.G, test.G, "G differs"); diff --git a/HPPH/Generated/HPPH.Generators/HPPH.Generators.ColorSortSourceGenerator/PixelHelper.g.cs b/HPPH/Generated/HPPH.Generators/HPPH.Generators.ColorSortSourceGenerator/PixelHelper.g.cs index da7195d..6e12764 100644 --- a/HPPH/Generated/HPPH.Generators/HPPH.Generators.ColorSortSourceGenerator/PixelHelper.g.cs +++ b/HPPH/Generated/HPPH.Generators/HPPH.Generators.ColorSortSourceGenerator/PixelHelper.g.cs @@ -4,7 +4,7 @@ namespace HPPH; public static unsafe partial class PixelHelper { - public static partial void SortByRed(Span colors) where T : unmanaged, IColor + public static partial void SortByRed(this Span colors) where T : unmanaged, IColor { fixed (T* ptr = colors) { @@ -39,7 +39,7 @@ public static unsafe partial class PixelHelper } } } - public static partial void SortByGreen(Span colors) where T : unmanaged, IColor + public static partial void SortByGreen(this Span colors) where T : unmanaged, IColor { fixed (T* ptr = colors) { @@ -74,7 +74,7 @@ public static unsafe partial class PixelHelper } } } - public static partial void SortByBlue(Span colors) where T : unmanaged, IColor + public static partial void SortByBlue(this Span colors) where T : unmanaged, IColor { fixed (T* ptr = colors) { @@ -109,7 +109,7 @@ public static unsafe partial class PixelHelper } } } - public static partial void SortByAlpha(Span colors) where T : unmanaged, IColor + public static partial void SortByAlpha(this Span colors) where T : unmanaged, IColor { fixed (T* ptr = colors) { @@ -144,250 +144,5 @@ public static unsafe partial class PixelHelper } } } - internal static partial void SortB1(Span colors) - { - fixed (Generic3ByteData* ptr = colors) - { - Generic3ByteData* end = ptr + colors.Length; - - Span histogram = stackalloc int[256]; - histogram.Clear(); - for (Generic3ByteData* color = ptr; color < end; color++) - histogram[(*color).B1]++; - - Generic3ByteData[] bucketsArray = ArrayPool.Shared.Rent(colors.Length); - try - { - Span buckets = bucketsArray.AsSpan()[..colors.Length]; - Span currentBucketIndex = stackalloc int[256]; - - int offset = 0; - for (int i = 0; i < histogram.Length; i++) - { - currentBucketIndex[i] = offset; - offset += histogram[i]; - } - - for (Generic3ByteData* color = ptr; color < end; color++) - buckets[currentBucketIndex[(*color).B1]++] = (*color); - - buckets.CopyTo(colors); - } - finally - { - ArrayPool.Shared.Return(bucketsArray); - } - } - } - internal static partial void SortB2(Span colors) - { - fixed (Generic3ByteData* ptr = colors) - { - Generic3ByteData* end = ptr + colors.Length; - - Span histogram = stackalloc int[256]; - histogram.Clear(); - for (Generic3ByteData* color = ptr; color < end; color++) - histogram[(*color).B2]++; - - Generic3ByteData[] bucketsArray = ArrayPool.Shared.Rent(colors.Length); - try - { - Span buckets = bucketsArray.AsSpan()[..colors.Length]; - Span currentBucketIndex = stackalloc int[256]; - - int offset = 0; - for (int i = 0; i < histogram.Length; i++) - { - currentBucketIndex[i] = offset; - offset += histogram[i]; - } - - for (Generic3ByteData* color = ptr; color < end; color++) - buckets[currentBucketIndex[(*color).B2]++] = (*color); - - buckets.CopyTo(colors); - } - finally - { - ArrayPool.Shared.Return(bucketsArray); - } - } - } - internal static partial void SortB3(Span colors) - { - fixed (Generic3ByteData* ptr = colors) - { - Generic3ByteData* end = ptr + colors.Length; - - Span histogram = stackalloc int[256]; - histogram.Clear(); - for (Generic3ByteData* color = ptr; color < end; color++) - histogram[(*color).B3]++; - - Generic3ByteData[] bucketsArray = ArrayPool.Shared.Rent(colors.Length); - try - { - Span buckets = bucketsArray.AsSpan()[..colors.Length]; - Span currentBucketIndex = stackalloc int[256]; - - int offset = 0; - for (int i = 0; i < histogram.Length; i++) - { - currentBucketIndex[i] = offset; - offset += histogram[i]; - } - - for (Generic3ByteData* color = ptr; color < end; color++) - buckets[currentBucketIndex[(*color).B3]++] = (*color); - - buckets.CopyTo(colors); - } - finally - { - ArrayPool.Shared.Return(bucketsArray); - } - } - } - internal static partial void SortB1(Span colors) - { - fixed (Generic4ByteData* ptr = colors) - { - Generic4ByteData* end = ptr + colors.Length; - - Span histogram = stackalloc int[256]; - histogram.Clear(); - for (Generic4ByteData* color = ptr; color < end; color++) - histogram[(*color).B1]++; - - Generic4ByteData[] bucketsArray = ArrayPool.Shared.Rent(colors.Length); - try - { - Span buckets = bucketsArray.AsSpan()[..colors.Length]; - Span currentBucketIndex = stackalloc int[256]; - - int offset = 0; - for (int i = 0; i < histogram.Length; i++) - { - currentBucketIndex[i] = offset; - offset += histogram[i]; - } - - for (Generic4ByteData* color = ptr; color < end; color++) - buckets[currentBucketIndex[(*color).B1]++] = (*color); - - buckets.CopyTo(colors); - } - finally - { - ArrayPool.Shared.Return(bucketsArray); - } - } - } - internal static partial void SortB2(Span colors) - { - fixed (Generic4ByteData* ptr = colors) - { - Generic4ByteData* end = ptr + colors.Length; - - Span histogram = stackalloc int[256]; - histogram.Clear(); - for (Generic4ByteData* color = ptr; color < end; color++) - histogram[(*color).B2]++; - - Generic4ByteData[] bucketsArray = ArrayPool.Shared.Rent(colors.Length); - try - { - Span buckets = bucketsArray.AsSpan()[..colors.Length]; - Span currentBucketIndex = stackalloc int[256]; - - int offset = 0; - for (int i = 0; i < histogram.Length; i++) - { - currentBucketIndex[i] = offset; - offset += histogram[i]; - } - - for (Generic4ByteData* color = ptr; color < end; color++) - buckets[currentBucketIndex[(*color).B2]++] = (*color); - - buckets.CopyTo(colors); - } - finally - { - ArrayPool.Shared.Return(bucketsArray); - } - } - } - internal static partial void SortB3(Span colors) - { - fixed (Generic4ByteData* ptr = colors) - { - Generic4ByteData* end = ptr + colors.Length; - - Span histogram = stackalloc int[256]; - histogram.Clear(); - for (Generic4ByteData* color = ptr; color < end; color++) - histogram[(*color).B3]++; - - Generic4ByteData[] bucketsArray = ArrayPool.Shared.Rent(colors.Length); - try - { - Span buckets = bucketsArray.AsSpan()[..colors.Length]; - Span currentBucketIndex = stackalloc int[256]; - - int offset = 0; - for (int i = 0; i < histogram.Length; i++) - { - currentBucketIndex[i] = offset; - offset += histogram[i]; - } - - for (Generic4ByteData* color = ptr; color < end; color++) - buckets[currentBucketIndex[(*color).B3]++] = (*color); - - buckets.CopyTo(colors); - } - finally - { - ArrayPool.Shared.Return(bucketsArray); - } - } - } - internal static partial void SortB4(Span colors) - { - fixed (Generic4ByteData* ptr = colors) - { - Generic4ByteData* end = ptr + colors.Length; - - Span histogram = stackalloc int[256]; - histogram.Clear(); - for (Generic4ByteData* color = ptr; color < end; color++) - histogram[(*color).B4]++; - - Generic4ByteData[] bucketsArray = ArrayPool.Shared.Rent(colors.Length); - try - { - Span buckets = bucketsArray.AsSpan()[..colors.Length]; - Span currentBucketIndex = stackalloc int[256]; - - int offset = 0; - for (int i = 0; i < histogram.Length; i++) - { - currentBucketIndex[i] = offset; - offset += histogram[i]; - } - - for (Generic4ByteData* color = ptr; color < end; color++) - buckets[currentBucketIndex[(*color).B4]++] = (*color); - - buckets.CopyTo(colors); - } - finally - { - ArrayPool.Shared.Return(bucketsArray); - } - } - } } \ No newline at end of file diff --git a/HPPH/PixelHelper.Average.cs b/HPPH/PixelHelper.Average.cs index dd3d3c3..06fea55 100644 --- a/HPPH/PixelHelper.Average.cs +++ b/HPPH/PixelHelper.Average.cs @@ -8,7 +8,7 @@ public static partial class PixelHelper { #region Methods - public static IColor Average(IImage image) + public static IColor Average(this IImage image) { ArgumentNullException.ThrowIfNull(image); @@ -26,7 +26,7 @@ public static partial class PixelHelper } } - public static T Average(RefImage image) + public static T Average(this RefImage image) where T : struct, IColor { int dataLength = image.Width * image.Height; @@ -43,7 +43,7 @@ public static partial class PixelHelper } } - public static T Average(ReadOnlySpan colors) + public static T Average(this ReadOnlySpan colors) where T : struct, IColor { if (colors == null) throw new ArgumentNullException(nameof(colors)); diff --git a/HPPH/PixelHelper.Convert.cs b/HPPH/PixelHelper.Convert.cs index a88974e..405ab26 100644 --- a/HPPH/PixelHelper.Convert.cs +++ b/HPPH/PixelHelper.Convert.cs @@ -7,9 +7,7 @@ public static unsafe partial class PixelHelper { #region Methods - #region In-Place - - public static Span ConvertInPlace(Span colors) + public static Span ConvertInPlace(this Span colors) where TSource : struct, IColor where TTarget : struct, IColor { @@ -28,11 +26,7 @@ public static unsafe partial class PixelHelper return MemoryMarshal.Cast(data); } - #endregion - - #region Allocating - - public static TTarget[] Convert(Span colors) + public static TTarget[] Convert(this Span colors) where TSource : struct, IColor where TTarget : struct, IColor { @@ -43,7 +37,7 @@ public static unsafe partial class PixelHelper return buffer; } - public static TTarget[] Convert(ReadOnlySpan colors) + public static TTarget[] Convert(this ReadOnlySpan colors) where TSource : struct, IColor where TTarget : struct, IColor { @@ -54,7 +48,7 @@ public static unsafe partial class PixelHelper return buffer; } - public static void Convert(ReadOnlySpan source, Span target) + public static void Convert(this ReadOnlySpan source, Span target) where TSource : struct, IColor where TTarget : struct, IColor { @@ -366,6 +360,4 @@ public static unsafe partial class PixelHelper } #endregion - - #endregion } diff --git a/HPPH/PixelHelper.MinMax.cs b/HPPH/PixelHelper.MinMax.cs index 6ba4476..9d72343 100644 --- a/HPPH/PixelHelper.MinMax.cs +++ b/HPPH/PixelHelper.MinMax.cs @@ -9,7 +9,7 @@ public static unsafe partial class PixelHelper { #region Methods - public static IMinMax MinMax(IImage image) + public static IMinMax MinMax(this IImage image) { ArgumentNullException.ThrowIfNull(image); @@ -27,7 +27,7 @@ public static unsafe partial class PixelHelper } } - public static IMinMax MinMax(RefImage image) + public static IMinMax MinMax(this RefImage image) where T : struct, IColor { int dataLength = image.Width * image.Height; @@ -44,7 +44,7 @@ public static unsafe partial class PixelHelper } } - public static IMinMax MinMax(ReadOnlySpan colors) + public static IMinMax MinMax(this ReadOnlySpan colors) where T : struct, IColor => T.ColorFormat.MinMax(MemoryMarshal.AsBytes(colors)); diff --git a/HPPH/PixelHelper.Quantize.cs b/HPPH/PixelHelper.Quantize.cs index 85f4618..7f00fc9 100644 --- a/HPPH/PixelHelper.Quantize.cs +++ b/HPPH/PixelHelper.Quantize.cs @@ -7,7 +7,7 @@ public static partial class PixelHelper { #region Methods - public static IColor[] CreateColorPalette(IImage image, int paletteSize) + public static IColor[] CreateColorPalette(this IImage image, int paletteSize) { ArgumentNullException.ThrowIfNull(image); @@ -25,7 +25,7 @@ public static partial class PixelHelper } } - public static T[] CreateColorPalette(RefImage image, int paletteSize) + public static T[] CreateColorPalette(this RefImage image, int paletteSize) where T : unmanaged, IColor { int dataLength = image.Width * image.Height; @@ -42,7 +42,7 @@ public static partial class PixelHelper } } - public static T[] CreateColorPalette(ReadOnlySpan colors, int paletteSize) + public static T[] CreateColorPalette(this ReadOnlySpan colors, int paletteSize) where T : unmanaged, IColor { T[] buffer = ArrayPool.Shared.Rent(colors.Length); @@ -59,7 +59,7 @@ public static partial class PixelHelper } } - public static T[] CreateColorPalette(Span colors, int paletteSize) + public static T[] CreateColorPalette(this Span colors, int paletteSize) where T : unmanaged, IColor { int splits = BitOperations.Log2((uint)paletteSize); diff --git a/HPPH/PixelHelper.Sort.cs b/HPPH/PixelHelper.Sort.cs index a62a015..e19fc1a 100644 --- a/HPPH/PixelHelper.Sort.cs +++ b/HPPH/PixelHelper.Sort.cs @@ -6,41 +6,20 @@ public static unsafe partial class PixelHelper #region Methods [ColorSortGenerator("T", "R")] - public static partial void SortByRed(Span colors) + public static partial void SortByRed(this Span colors) where T : unmanaged, IColor; [ColorSortGenerator("T", "G")] - public static partial void SortByGreen(Span colors) + public static partial void SortByGreen(this Span colors) where T : unmanaged, IColor; [ColorSortGenerator("T", "B")] - public static partial void SortByBlue(Span colors) + public static partial void SortByBlue(this Span colors) where T : unmanaged, IColor; [ColorSortGenerator("T", "A")] - public static partial void SortByAlpha(Span colors) + public static partial void SortByAlpha(this Span colors) where T : unmanaged, IColor; - [ColorSortGenerator("Generic3ByteData", "B1")] - internal static partial void SortB1(Span colors); - - [ColorSortGenerator("Generic3ByteData", "B2")] - internal static partial void SortB2(Span colors); - - [ColorSortGenerator("Generic3ByteData", "B3")] - internal static partial void SortB3(Span colors); - - [ColorSortGenerator("Generic4ByteData", "B1")] - internal static partial void SortB1(Span colors); - - [ColorSortGenerator("Generic4ByteData", "B2")] - internal static partial void SortB2(Span colors); - - [ColorSortGenerator("Generic4ByteData", "B3")] - internal static partial void SortB3(Span colors); - - [ColorSortGenerator("Generic4ByteData", "B4")] - internal static partial void SortB4(Span colors); - #endregion } diff --git a/HPPH/PixelHelper.Sum.cs b/HPPH/PixelHelper.Sum.cs index 334cbb3..fd6211c 100644 --- a/HPPH/PixelHelper.Sum.cs +++ b/HPPH/PixelHelper.Sum.cs @@ -10,7 +10,7 @@ public static unsafe partial class PixelHelper { #region Methods - public static ISum Sum(IImage image) + public static ISum Sum(this IImage image) { ArgumentNullException.ThrowIfNull(image); @@ -28,7 +28,7 @@ public static unsafe partial class PixelHelper } } - public static ISum Sum(RefImage image) + public static ISum Sum(this RefImage image) where T : struct, IColor { int dataLength = image.Width * image.Height; @@ -45,7 +45,7 @@ public static unsafe partial class PixelHelper } } - public static ISum Sum(ReadOnlySpan colors) + public static ISum Sum(this ReadOnlySpan colors) where T : struct, IColor => T.ColorFormat.Sum(MemoryMarshal.AsBytes(colors));