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

This commit is contained in:
DarthAffe committed 2024-07-08 23:55:48 +02:00
1 parent fb56257e87
commit 132bf7f54a
13 files changed
+53 -327

No files matched your search

@@ -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);
}
}
}
}
+3 -3
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));
+4 -12
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
}
+3 -3
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));
+4 -4
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);
+4 -25
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
}
+3 -3
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));