Changed all public PixelHelper-methods to be extensions; removed unused sorts

This commit is contained in:
Darth Affe 2024-07-08 23:55:48 +02:00
parent fb56257e87
commit 132bf7f54a
13 changed files with 53 additions and 327 deletions

View File

@ -26,7 +26,7 @@ public class AverageTests
ReadOnlySpan<ColorRGB> 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<ColorRGBA> 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");

View File

@ -18,7 +18,7 @@ public class ConvertTests
Span<ColorRGB> sourceData = new ColorRGB[referenceData.Length];
referenceData.CopyTo(sourceData);
Span<ColorBGR> result = PixelHelper.Convert<ColorRGB, ColorBGR>(sourceData);
Span<ColorBGR> result = sourceData.Convert<ColorRGB, ColorBGR>();
Assert.AreEqual(referenceData.Length, result.Length);
for (int i = 0; i < referenceData.Length; i++)
@ -48,7 +48,7 @@ public class ConvertTests
Span<ColorRGBA> sourceData = new ColorRGBA[referenceData.Length];
referenceData.CopyTo(sourceData);
Span<ColorARGB> result = PixelHelper.Convert<ColorRGBA, ColorARGB>(sourceData);
Span<ColorARGB> result = sourceData.Convert<ColorRGBA, ColorARGB>();
Assert.AreEqual(referenceData.Length, result.Length);
for (int i = 0; i < referenceData.Length; i++)
@ -78,7 +78,7 @@ public class ConvertTests
Span<ColorRGBA> sourceData = new ColorRGBA[referenceData.Length];
referenceData.CopyTo(sourceData);
Span<ColorBGRA> result = PixelHelper.Convert<ColorRGBA, ColorBGRA>(sourceData);
Span<ColorBGRA> result = sourceData.Convert<ColorRGBA, ColorBGRA>();
Assert.AreEqual(referenceData.Length, result.Length);
for (int i = 0; i < referenceData.Length; i++)
@ -108,7 +108,7 @@ public class ConvertTests
Span<ColorRGBA> sourceData = new ColorRGBA[referenceData.Length];
referenceData.CopyTo(sourceData);
Span<ColorRGB> result = PixelHelper.Convert<ColorRGBA, ColorRGB>(sourceData);
Span<ColorRGB> result = sourceData.Convert<ColorRGBA, ColorRGB>();
Assert.AreEqual(referenceData.Length, result.Length);
for (int i = 0; i < referenceData.Length; i++)
@ -138,7 +138,7 @@ public class ConvertTests
Span<ColorRGBA> sourceData = new ColorRGBA[referenceData.Length];
referenceData.CopyTo(sourceData);
Span<ColorBGR> result = PixelHelper.Convert<ColorRGBA, ColorBGR>(sourceData);
Span<ColorBGR> result = sourceData.Convert<ColorRGBA, ColorBGR>();
Assert.AreEqual(referenceData.Length, result.Length);
for (int i = 0; i < referenceData.Length; i++)
@ -168,7 +168,7 @@ public class ConvertTests
Span<ColorRGB> sourceData = new ColorRGB[referenceData.Length];
referenceData.CopyTo(sourceData);
Span<ColorRGBA> result = PixelHelper.Convert<ColorRGB, ColorRGBA>(sourceData);
Span<ColorRGBA> result = sourceData.Convert<ColorRGB, ColorRGBA>();
Assert.AreEqual(referenceData.Length, result.Length);
for (int i = 0; i < referenceData.Length; i++)
@ -198,7 +198,7 @@ public class ConvertTests
Span<ColorRGB> sourceData = new ColorRGB[referenceData.Length];
referenceData.CopyTo(sourceData);
Span<ColorARGB> result = PixelHelper.Convert<ColorRGB, ColorARGB>(sourceData);
Span<ColorARGB> result = sourceData.Convert<ColorRGB, ColorARGB>();
Assert.AreEqual(referenceData.Length, result.Length);
for (int i = 0; i < referenceData.Length; i++)
@ -228,7 +228,7 @@ public class ConvertTests
Span<ColorRGB> sourceData = new ColorRGB[referenceData.Length];
referenceData.CopyTo(sourceData);
Span<ColorBGRA> result = PixelHelper.Convert<ColorRGB, ColorBGRA>(sourceData);
Span<ColorBGRA> result = sourceData.Convert<ColorRGB, ColorBGRA>();
Assert.AreEqual(referenceData.Length, result.Length);
for (int i = 0; i < referenceData.Length; i++)
@ -258,7 +258,7 @@ public class ConvertTests
Span<ColorRGB> sourceData = new ColorRGB[referenceData.Length];
referenceData.CopyTo(sourceData);
Span<ColorABGR> result = PixelHelper.Convert<ColorRGB, ColorABGR>(sourceData);
Span<ColorABGR> result = sourceData.Convert<ColorRGB, ColorABGR>();
Assert.AreEqual(referenceData.Length, result.Length);
for (int i = 0; i < referenceData.Length; i++)

View File

@ -26,7 +26,7 @@ public class MinMaxTests
ReadOnlySpan<ColorRGB> 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<ColorRGBA> 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");

View File

@ -16,7 +16,7 @@ public class CreateColorPaletteTests
Span<ColorRGB> 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<ColorRGB> 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<ColorRGB> 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<ColorRGBA> 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<ColorRGBA> 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<ColorRGBA> 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");

View File

@ -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");

View File

@ -26,7 +26,7 @@ public class SumTests
ReadOnlySpan<ColorRGB> 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<ColorRGBA> 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");

View File

@ -4,7 +4,7 @@ namespace HPPH;
public static unsafe partial class PixelHelper
{
public static partial void SortByRed<T>(Span<T> colors) where T : unmanaged, IColor
public static partial void SortByRed<T>(this Span<T> colors) where T : unmanaged, IColor
{
fixed (T* ptr = colors)
{
@ -39,7 +39,7 @@ public static unsafe partial class PixelHelper
}
}
}
public static partial void SortByGreen<T>(Span<T> colors) where T : unmanaged, IColor
public static partial void SortByGreen<T>(this Span<T> colors) where T : unmanaged, IColor
{
fixed (T* ptr = colors)
{
@ -74,7 +74,7 @@ public static unsafe partial class PixelHelper
}
}
}
public static partial void SortByBlue<T>(Span<T> colors) where T : unmanaged, IColor
public static partial void SortByBlue<T>(this Span<T> colors) where T : unmanaged, IColor
{
fixed (T* ptr = colors)
{
@ -109,7 +109,7 @@ public static unsafe partial class PixelHelper
}
}
}
public static partial void SortByAlpha<T>(Span<T> colors) where T : unmanaged, IColor
public static partial void SortByAlpha<T>(this Span<T> 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<Generic3ByteData> colors)
{
fixed (Generic3ByteData* ptr = colors)
{
Generic3ByteData* end = ptr + colors.Length;
Span<int> histogram = stackalloc int[256];
histogram.Clear();
for (Generic3ByteData* color = ptr; color < end; color++)
histogram[(*color).B1]++;
Generic3ByteData[] bucketsArray = ArrayPool<Generic3ByteData>.Shared.Rent(colors.Length);
try
{
Span<Generic3ByteData> buckets = bucketsArray.AsSpan()[..colors.Length];
Span<int> 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<Generic3ByteData>.Shared.Return(bucketsArray);
}
}
}
internal static partial void SortB2(Span<Generic3ByteData> colors)
{
fixed (Generic3ByteData* ptr = colors)
{
Generic3ByteData* end = ptr + colors.Length;
Span<int> histogram = stackalloc int[256];
histogram.Clear();
for (Generic3ByteData* color = ptr; color < end; color++)
histogram[(*color).B2]++;
Generic3ByteData[] bucketsArray = ArrayPool<Generic3ByteData>.Shared.Rent(colors.Length);
try
{
Span<Generic3ByteData> buckets = bucketsArray.AsSpan()[..colors.Length];
Span<int> 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<Generic3ByteData>.Shared.Return(bucketsArray);
}
}
}
internal static partial void SortB3(Span<Generic3ByteData> colors)
{
fixed (Generic3ByteData* ptr = colors)
{
Generic3ByteData* end = ptr + colors.Length;
Span<int> histogram = stackalloc int[256];
histogram.Clear();
for (Generic3ByteData* color = ptr; color < end; color++)
histogram[(*color).B3]++;
Generic3ByteData[] bucketsArray = ArrayPool<Generic3ByteData>.Shared.Rent(colors.Length);
try
{
Span<Generic3ByteData> buckets = bucketsArray.AsSpan()[..colors.Length];
Span<int> 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<Generic3ByteData>.Shared.Return(bucketsArray);
}
}
}
internal static partial void SortB1(Span<Generic4ByteData> colors)
{
fixed (Generic4ByteData* ptr = colors)
{
Generic4ByteData* end = ptr + colors.Length;
Span<int> histogram = stackalloc int[256];
histogram.Clear();
for (Generic4ByteData* color = ptr; color < end; color++)
histogram[(*color).B1]++;
Generic4ByteData[] bucketsArray = ArrayPool<Generic4ByteData>.Shared.Rent(colors.Length);
try
{
Span<Generic4ByteData> buckets = bucketsArray.AsSpan()[..colors.Length];
Span<int> 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<Generic4ByteData>.Shared.Return(bucketsArray);
}
}
}
internal static partial void SortB2(Span<Generic4ByteData> colors)
{
fixed (Generic4ByteData* ptr = colors)
{
Generic4ByteData* end = ptr + colors.Length;
Span<int> histogram = stackalloc int[256];
histogram.Clear();
for (Generic4ByteData* color = ptr; color < end; color++)
histogram[(*color).B2]++;
Generic4ByteData[] bucketsArray = ArrayPool<Generic4ByteData>.Shared.Rent(colors.Length);
try
{
Span<Generic4ByteData> buckets = bucketsArray.AsSpan()[..colors.Length];
Span<int> 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<Generic4ByteData>.Shared.Return(bucketsArray);
}
}
}
internal static partial void SortB3(Span<Generic4ByteData> colors)
{
fixed (Generic4ByteData* ptr = colors)
{
Generic4ByteData* end = ptr + colors.Length;
Span<int> histogram = stackalloc int[256];
histogram.Clear();
for (Generic4ByteData* color = ptr; color < end; color++)
histogram[(*color).B3]++;
Generic4ByteData[] bucketsArray = ArrayPool<Generic4ByteData>.Shared.Rent(colors.Length);
try
{
Span<Generic4ByteData> buckets = bucketsArray.AsSpan()[..colors.Length];
Span<int> 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<Generic4ByteData>.Shared.Return(bucketsArray);
}
}
}
internal static partial void SortB4(Span<Generic4ByteData> colors)
{
fixed (Generic4ByteData* ptr = colors)
{
Generic4ByteData* end = ptr + colors.Length;
Span<int> histogram = stackalloc int[256];
histogram.Clear();
for (Generic4ByteData* color = ptr; color < end; color++)
histogram[(*color).B4]++;
Generic4ByteData[] bucketsArray = ArrayPool<Generic4ByteData>.Shared.Rent(colors.Length);
try
{
Span<Generic4ByteData> buckets = bucketsArray.AsSpan()[..colors.Length];
Span<int> 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<Generic4ByteData>.Shared.Return(bucketsArray);
}
}
}
}

View File

@ -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<T>(RefImage<T> image)
public static T Average<T>(this RefImage<T> image)
where T : struct, IColor
{
int dataLength = image.Width * image.Height;
@ -43,7 +43,7 @@ public static partial class PixelHelper
}
}
public static T Average<T>(ReadOnlySpan<T> colors)
public static T Average<T>(this ReadOnlySpan<T> colors)
where T : struct, IColor
{
if (colors == null) throw new ArgumentNullException(nameof(colors));

View File

@ -7,9 +7,7 @@ public static unsafe partial class PixelHelper
{
#region Methods
#region In-Place
public static Span<TTarget> ConvertInPlace<TSource, TTarget>(Span<TSource> colors)
public static Span<TTarget> ConvertInPlace<TSource, TTarget>(this Span<TSource> colors)
where TSource : struct, IColor
where TTarget : struct, IColor
{
@ -28,11 +26,7 @@ public static unsafe partial class PixelHelper
return MemoryMarshal.Cast<byte, TTarget>(data);
}
#endregion
#region Allocating
public static TTarget[] Convert<TSource, TTarget>(Span<TSource> colors)
public static TTarget[] Convert<TSource, TTarget>(this Span<TSource> 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<TSource, TTarget>(ReadOnlySpan<TSource> colors)
public static TTarget[] Convert<TSource, TTarget>(this ReadOnlySpan<TSource> 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<TSource, TTarget>(ReadOnlySpan<TSource> source, Span<TTarget> target)
public static void Convert<TSource, TTarget>(this ReadOnlySpan<TSource> source, Span<TTarget> target)
where TSource : struct, IColor
where TTarget : struct, IColor
{
@ -366,6 +360,4 @@ public static unsafe partial class PixelHelper
}
#endregion
#endregion
}

View File

@ -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<T>(RefImage<T> image)
public static IMinMax MinMax<T>(this RefImage<T> 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<T>(ReadOnlySpan<T> colors)
public static IMinMax MinMax<T>(this ReadOnlySpan<T> colors)
where T : struct, IColor
=> T.ColorFormat.MinMax(MemoryMarshal.AsBytes(colors));

View File

@ -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<T>(RefImage<T> image, int paletteSize)
public static T[] CreateColorPalette<T>(this RefImage<T> 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<T>(ReadOnlySpan<T> colors, int paletteSize)
public static T[] CreateColorPalette<T>(this ReadOnlySpan<T> colors, int paletteSize)
where T : unmanaged, IColor
{
T[] buffer = ArrayPool<T>.Shared.Rent(colors.Length);
@ -59,7 +59,7 @@ public static partial class PixelHelper
}
}
public static T[] CreateColorPalette<T>(Span<T> colors, int paletteSize)
public static T[] CreateColorPalette<T>(this Span<T> colors, int paletteSize)
where T : unmanaged, IColor
{
int splits = BitOperations.Log2((uint)paletteSize);

View File

@ -6,41 +6,20 @@ public static unsafe partial class PixelHelper
#region Methods
[ColorSortGenerator("T", "R")]
public static partial void SortByRed<T>(Span<T> colors)
public static partial void SortByRed<T>(this Span<T> colors)
where T : unmanaged, IColor;
[ColorSortGenerator("T", "G")]
public static partial void SortByGreen<T>(Span<T> colors)
public static partial void SortByGreen<T>(this Span<T> colors)
where T : unmanaged, IColor;
[ColorSortGenerator("T", "B")]
public static partial void SortByBlue<T>(Span<T> colors)
public static partial void SortByBlue<T>(this Span<T> colors)
where T : unmanaged, IColor;
[ColorSortGenerator("T", "A")]
public static partial void SortByAlpha<T>(Span<T> colors)
public static partial void SortByAlpha<T>(this Span<T> colors)
where T : unmanaged, IColor;
[ColorSortGenerator("Generic3ByteData", "B1")]
internal static partial void SortB1(Span<Generic3ByteData> colors);
[ColorSortGenerator("Generic3ByteData", "B2")]
internal static partial void SortB2(Span<Generic3ByteData> colors);
[ColorSortGenerator("Generic3ByteData", "B3")]
internal static partial void SortB3(Span<Generic3ByteData> colors);
[ColorSortGenerator("Generic4ByteData", "B1")]
internal static partial void SortB1(Span<Generic4ByteData> colors);
[ColorSortGenerator("Generic4ByteData", "B2")]
internal static partial void SortB2(Span<Generic4ByteData> colors);
[ColorSortGenerator("Generic4ByteData", "B3")]
internal static partial void SortB3(Span<Generic4ByteData> colors);
[ColorSortGenerator("Generic4ByteData", "B4")]
internal static partial void SortB4(Span<Generic4ByteData> colors);
#endregion
}

View File

@ -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<T>(RefImage<T> image)
public static ISum Sum<T>(this RefImage<T> 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<T>(ReadOnlySpan<T> colors)
public static ISum Sum<T>(this ReadOnlySpan<T> colors)
where T : struct, IColor
=> T.ColorFormat.Sum(MemoryMarshal.AsBytes(colors));