mirror of
https://github.com/DarthAffe/HPPH.git
synced 2025-12-12 21:38:59 +00:00
Added special case for small amounts of data for all copying helpers
This commit is contained in:
parent
cfdd95f079
commit
bd7a2bca48
@ -13,6 +13,16 @@ public static partial class PixelHelper
|
|||||||
ArgumentNullException.ThrowIfNull(image);
|
ArgumentNullException.ThrowIfNull(image);
|
||||||
|
|
||||||
int dataLength = image.SizeInBytes;
|
int dataLength = image.SizeInBytes;
|
||||||
|
|
||||||
|
if (dataLength <= 1024)
|
||||||
|
{
|
||||||
|
Span<byte> buffer = stackalloc byte[dataLength];
|
||||||
|
|
||||||
|
image.CopyTo(buffer);
|
||||||
|
return image.ColorFormat.Average(buffer);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
byte[] array = ArrayPool<byte>.Shared.Rent(dataLength);
|
byte[] array = ArrayPool<byte>.Shared.Rent(dataLength);
|
||||||
Span<byte> buffer = array.AsSpan()[..dataLength];
|
Span<byte> buffer = array.AsSpan()[..dataLength];
|
||||||
try
|
try
|
||||||
@ -25,6 +35,7 @@ public static partial class PixelHelper
|
|||||||
ArrayPool<byte>.Shared.Return(array);
|
ArrayPool<byte>.Shared.Return(array);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static T Average<T>(this IImage<T> image)
|
public static T Average<T>(this IImage<T> image)
|
||||||
where T : struct, IColor
|
where T : struct, IColor
|
||||||
@ -34,8 +45,19 @@ public static partial class PixelHelper
|
|||||||
where T : struct, IColor
|
where T : struct, IColor
|
||||||
{
|
{
|
||||||
int dataLength = image.Width * image.Height;
|
int dataLength = image.Width * image.Height;
|
||||||
|
int sizeInBytes = dataLength * T.ColorFormat.BytesPerPixel;
|
||||||
|
|
||||||
|
if (sizeInBytes <= 1024)
|
||||||
|
{
|
||||||
|
Span<T> buffer = MemoryMarshal.Cast<byte, T>(stackalloc byte[sizeInBytes]);
|
||||||
|
|
||||||
|
image.CopyTo(buffer);
|
||||||
|
return Average(buffer);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
T[] array = ArrayPool<T>.Shared.Rent(dataLength);
|
T[] array = ArrayPool<T>.Shared.Rent(dataLength);
|
||||||
Span<T> buffer = array.AsSpan()[..(dataLength)];
|
Span<T> buffer = array.AsSpan()[..dataLength];
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
image.CopyTo(buffer);
|
image.CopyTo(buffer);
|
||||||
@ -46,6 +68,7 @@ public static partial class PixelHelper
|
|||||||
ArrayPool<T>.Shared.Return(array);
|
ArrayPool<T>.Shared.Return(array);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static T Average<T>(this Span<T> colors)
|
public static T Average<T>(this Span<T> colors)
|
||||||
where T : struct, IColor
|
where T : struct, IColor
|
||||||
|
|||||||
@ -14,6 +14,16 @@ public static unsafe partial class PixelHelper
|
|||||||
ArgumentNullException.ThrowIfNull(image);
|
ArgumentNullException.ThrowIfNull(image);
|
||||||
|
|
||||||
int dataLength = image.SizeInBytes;
|
int dataLength = image.SizeInBytes;
|
||||||
|
|
||||||
|
if (dataLength <= 1024)
|
||||||
|
{
|
||||||
|
Span<byte> buffer = stackalloc byte[dataLength];
|
||||||
|
|
||||||
|
image.CopyTo(buffer);
|
||||||
|
return image.ColorFormat.MinMax(buffer);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
byte[] array = ArrayPool<byte>.Shared.Rent(dataLength);
|
byte[] array = ArrayPool<byte>.Shared.Rent(dataLength);
|
||||||
Span<byte> buffer = array.AsSpan()[..dataLength];
|
Span<byte> buffer = array.AsSpan()[..dataLength];
|
||||||
try
|
try
|
||||||
@ -26,6 +36,7 @@ public static unsafe partial class PixelHelper
|
|||||||
ArrayPool<byte>.Shared.Return(array);
|
ArrayPool<byte>.Shared.Return(array);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static IMinMax MinMax<T>(this IImage<T> image)
|
public static IMinMax MinMax<T>(this IImage<T> image)
|
||||||
where T : struct, IColor
|
where T : struct, IColor
|
||||||
@ -35,6 +46,17 @@ public static unsafe partial class PixelHelper
|
|||||||
where T : struct, IColor
|
where T : struct, IColor
|
||||||
{
|
{
|
||||||
int dataLength = image.Width * image.Height;
|
int dataLength = image.Width * image.Height;
|
||||||
|
int sizeInBytes = dataLength * T.ColorFormat.BytesPerPixel;
|
||||||
|
|
||||||
|
if (sizeInBytes <= 1024)
|
||||||
|
{
|
||||||
|
Span<T> buffer = MemoryMarshal.Cast<byte, T>(stackalloc byte[sizeInBytes]);
|
||||||
|
|
||||||
|
image.CopyTo(buffer);
|
||||||
|
return MinMax(buffer);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
T[] array = ArrayPool<T>.Shared.Rent(dataLength);
|
T[] array = ArrayPool<T>.Shared.Rent(dataLength);
|
||||||
Span<T> buffer = array.AsSpan()[..(dataLength)];
|
Span<T> buffer = array.AsSpan()[..(dataLength)];
|
||||||
try
|
try
|
||||||
@ -47,6 +69,7 @@ public static unsafe partial class PixelHelper
|
|||||||
ArrayPool<T>.Shared.Return(array);
|
ArrayPool<T>.Shared.Return(array);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
public static IMinMax MinMax<T>(this Span<T> colors)
|
public static IMinMax MinMax<T>(this Span<T> colors)
|
||||||
where T : struct, IColor
|
where T : struct, IColor
|
||||||
=> T.ColorFormat.MinMax(MemoryMarshal.AsBytes(colors));
|
=> T.ColorFormat.MinMax(MemoryMarshal.AsBytes(colors));
|
||||||
|
|||||||
@ -1,5 +1,7 @@
|
|||||||
using System.Buffers;
|
using System.Buffers;
|
||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
using static System.Net.Mime.MediaTypeNames;
|
||||||
|
|
||||||
namespace HPPH;
|
namespace HPPH;
|
||||||
|
|
||||||
@ -12,6 +14,16 @@ public static partial class PixelHelper
|
|||||||
ArgumentNullException.ThrowIfNull(image);
|
ArgumentNullException.ThrowIfNull(image);
|
||||||
|
|
||||||
int dataLength = image.SizeInBytes;
|
int dataLength = image.SizeInBytes;
|
||||||
|
|
||||||
|
if (dataLength <= 1024)
|
||||||
|
{
|
||||||
|
Span<byte> buffer = stackalloc byte[dataLength];
|
||||||
|
|
||||||
|
image.CopyTo(buffer);
|
||||||
|
return image.ColorFormat.CreateColorPalette(buffer, paletteSize);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
byte[] array = ArrayPool<byte>.Shared.Rent(dataLength);
|
byte[] array = ArrayPool<byte>.Shared.Rent(dataLength);
|
||||||
Span<byte> buffer = array.AsSpan()[..dataLength];
|
Span<byte> buffer = array.AsSpan()[..dataLength];
|
||||||
try
|
try
|
||||||
@ -24,6 +36,7 @@ public static partial class PixelHelper
|
|||||||
ArrayPool<byte>.Shared.Return(array);
|
ArrayPool<byte>.Shared.Return(array);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static T[] CreateColorPalette<T>(this IImage<T> image, int paletteSize)
|
public static T[] CreateColorPalette<T>(this IImage<T> image, int paletteSize)
|
||||||
where T : unmanaged, IColor
|
where T : unmanaged, IColor
|
||||||
@ -33,6 +46,17 @@ public static partial class PixelHelper
|
|||||||
where T : unmanaged, IColor
|
where T : unmanaged, IColor
|
||||||
{
|
{
|
||||||
int dataLength = image.Width * image.Height;
|
int dataLength = image.Width * image.Height;
|
||||||
|
int sizeInBytes = dataLength * T.ColorFormat.BytesPerPixel;
|
||||||
|
|
||||||
|
if (sizeInBytes <= 1024)
|
||||||
|
{
|
||||||
|
Span<T> buffer = MemoryMarshal.Cast<byte, T>(stackalloc byte[sizeInBytes]);
|
||||||
|
|
||||||
|
image.CopyTo(buffer);
|
||||||
|
return CreateColorPalette(buffer, paletteSize);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
T[] array = ArrayPool<T>.Shared.Rent(dataLength);
|
T[] array = ArrayPool<T>.Shared.Rent(dataLength);
|
||||||
Span<T> buffer = array.AsSpan()[..(dataLength)];
|
Span<T> buffer = array.AsSpan()[..(dataLength)];
|
||||||
try
|
try
|
||||||
@ -45,9 +69,21 @@ public static partial class PixelHelper
|
|||||||
ArrayPool<T>.Shared.Return(array);
|
ArrayPool<T>.Shared.Return(array);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static T[] CreateColorPalette<T>(this ReadOnlySpan<T> colors, int paletteSize)
|
public static T[] CreateColorPalette<T>(this ReadOnlySpan<T> colors, int paletteSize)
|
||||||
where T : unmanaged, IColor
|
where T : unmanaged, IColor
|
||||||
|
{
|
||||||
|
int sizeInBytes = colors.Length * T.ColorFormat.BytesPerPixel;
|
||||||
|
|
||||||
|
if (sizeInBytes <= 1024)
|
||||||
|
{
|
||||||
|
Span<T> buffer = MemoryMarshal.Cast<byte, T>(stackalloc byte[sizeInBytes]);
|
||||||
|
|
||||||
|
colors.CopyTo(buffer);
|
||||||
|
return CreateColorPalette(buffer, paletteSize);
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
T[] buffer = ArrayPool<T>.Shared.Rent(colors.Length);
|
T[] buffer = ArrayPool<T>.Shared.Rent(colors.Length);
|
||||||
try
|
try
|
||||||
@ -62,6 +98,7 @@ public static partial class PixelHelper
|
|||||||
ArrayPool<T>.Shared.Return(buffer);
|
ArrayPool<T>.Shared.Return(buffer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static T[] CreateColorPalette<T>(this Span<T> colors, int paletteSize)
|
public static T[] CreateColorPalette<T>(this Span<T> colors, int paletteSize)
|
||||||
where T : unmanaged, IColor
|
where T : unmanaged, IColor
|
||||||
@ -102,6 +139,16 @@ public static partial class PixelHelper
|
|||||||
ArgumentNullException.ThrowIfNull(image);
|
ArgumentNullException.ThrowIfNull(image);
|
||||||
|
|
||||||
int dataLength = image.SizeInBytes;
|
int dataLength = image.SizeInBytes;
|
||||||
|
|
||||||
|
if (dataLength <= 1024)
|
||||||
|
{
|
||||||
|
Span<byte> buffer = stackalloc byte[dataLength];
|
||||||
|
|
||||||
|
image.CopyTo(buffer);
|
||||||
|
return image.ColorFormat.CreateSimpleColorPalette(buffer, paletteSize);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
byte[] array = ArrayPool<byte>.Shared.Rent(dataLength);
|
byte[] array = ArrayPool<byte>.Shared.Rent(dataLength);
|
||||||
Span<byte> buffer = array.AsSpan()[..dataLength];
|
Span<byte> buffer = array.AsSpan()[..dataLength];
|
||||||
try
|
try
|
||||||
@ -114,6 +161,7 @@ public static partial class PixelHelper
|
|||||||
ArrayPool<byte>.Shared.Return(array);
|
ArrayPool<byte>.Shared.Return(array);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static T[] CreateSimpleColorPalette<T>(this IImage<T> image, int paletteSize)
|
public static T[] CreateSimpleColorPalette<T>(this IImage<T> image, int paletteSize)
|
||||||
where T : unmanaged, IColor
|
where T : unmanaged, IColor
|
||||||
@ -123,6 +171,17 @@ public static partial class PixelHelper
|
|||||||
where T : unmanaged, IColor
|
where T : unmanaged, IColor
|
||||||
{
|
{
|
||||||
int dataLength = image.Width * image.Height;
|
int dataLength = image.Width * image.Height;
|
||||||
|
int sizeInBytes = dataLength * T.ColorFormat.BytesPerPixel;
|
||||||
|
|
||||||
|
if (sizeInBytes <= 1024)
|
||||||
|
{
|
||||||
|
Span<T> buffer = MemoryMarshal.Cast<byte, T>(stackalloc byte[sizeInBytes]);
|
||||||
|
|
||||||
|
image.CopyTo(buffer);
|
||||||
|
return CreateSimpleColorPalette(buffer, paletteSize);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
T[] array = ArrayPool<T>.Shared.Rent(dataLength);
|
T[] array = ArrayPool<T>.Shared.Rent(dataLength);
|
||||||
Span<T> buffer = array.AsSpan()[..(dataLength)];
|
Span<T> buffer = array.AsSpan()[..(dataLength)];
|
||||||
try
|
try
|
||||||
@ -135,9 +194,21 @@ public static partial class PixelHelper
|
|||||||
ArrayPool<T>.Shared.Return(array);
|
ArrayPool<T>.Shared.Return(array);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static T[] CreateSimpleColorPalette<T>(this ReadOnlySpan<T> colors, int paletteSize)
|
public static T[] CreateSimpleColorPalette<T>(this ReadOnlySpan<T> colors, int paletteSize)
|
||||||
where T : unmanaged, IColor
|
where T : unmanaged, IColor
|
||||||
|
{
|
||||||
|
int sizeInBytes = colors.Length * T.ColorFormat.BytesPerPixel;
|
||||||
|
|
||||||
|
if (sizeInBytes <= 1024)
|
||||||
|
{
|
||||||
|
Span<T> buffer = MemoryMarshal.Cast<byte, T>(stackalloc byte[sizeInBytes]);
|
||||||
|
|
||||||
|
colors.CopyTo(buffer);
|
||||||
|
return CreateSimpleColorPalette(buffer, paletteSize);
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
T[] buffer = ArrayPool<T>.Shared.Rent(colors.Length);
|
T[] buffer = ArrayPool<T>.Shared.Rent(colors.Length);
|
||||||
try
|
try
|
||||||
@ -152,6 +223,7 @@ public static partial class PixelHelper
|
|||||||
ArrayPool<T>.Shared.Return(buffer);
|
ArrayPool<T>.Shared.Return(buffer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static unsafe T[] CreateSimpleColorPalette<T>(this Span<T> colors, int paletteSize)
|
public static unsafe T[] CreateSimpleColorPalette<T>(this Span<T> colors, int paletteSize)
|
||||||
where T : unmanaged, IColor
|
where T : unmanaged, IColor
|
||||||
|
|||||||
@ -15,6 +15,16 @@ public static unsafe partial class PixelHelper
|
|||||||
ArgumentNullException.ThrowIfNull(image);
|
ArgumentNullException.ThrowIfNull(image);
|
||||||
|
|
||||||
int dataLength = image.SizeInBytes;
|
int dataLength = image.SizeInBytes;
|
||||||
|
|
||||||
|
if (dataLength <= 1024)
|
||||||
|
{
|
||||||
|
Span<byte> buffer = stackalloc byte[dataLength];
|
||||||
|
|
||||||
|
image.CopyTo(buffer);
|
||||||
|
return image.ColorFormat.Sum(buffer);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
byte[] array = ArrayPool<byte>.Shared.Rent(dataLength);
|
byte[] array = ArrayPool<byte>.Shared.Rent(dataLength);
|
||||||
Span<byte> buffer = array.AsSpan()[..dataLength];
|
Span<byte> buffer = array.AsSpan()[..dataLength];
|
||||||
try
|
try
|
||||||
@ -27,6 +37,7 @@ public static unsafe partial class PixelHelper
|
|||||||
ArrayPool<byte>.Shared.Return(array);
|
ArrayPool<byte>.Shared.Return(array);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static ISum Sum<T>(this IImage<T> image)
|
public static ISum Sum<T>(this IImage<T> image)
|
||||||
where T : struct, IColor
|
where T : struct, IColor
|
||||||
@ -36,6 +47,17 @@ public static unsafe partial class PixelHelper
|
|||||||
where T : struct, IColor
|
where T : struct, IColor
|
||||||
{
|
{
|
||||||
int dataLength = image.Width * image.Height;
|
int dataLength = image.Width * image.Height;
|
||||||
|
int sizeInBytes = dataLength * T.ColorFormat.BytesPerPixel;
|
||||||
|
|
||||||
|
if (sizeInBytes <= 1024)
|
||||||
|
{
|
||||||
|
Span<T> buffer = MemoryMarshal.Cast<byte, T>(stackalloc byte[sizeInBytes]);
|
||||||
|
|
||||||
|
image.CopyTo(buffer);
|
||||||
|
return Sum(buffer);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
T[] array = ArrayPool<T>.Shared.Rent(dataLength);
|
T[] array = ArrayPool<T>.Shared.Rent(dataLength);
|
||||||
Span<T> buffer = array.AsSpan()[..(dataLength)];
|
Span<T> buffer = array.AsSpan()[..(dataLength)];
|
||||||
try
|
try
|
||||||
@ -48,6 +70,7 @@ public static unsafe partial class PixelHelper
|
|||||||
ArrayPool<T>.Shared.Return(array);
|
ArrayPool<T>.Shared.Return(array);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static ISum Sum<T>(this ReadOnlySpan<T> colors)
|
public static ISum Sum<T>(this ReadOnlySpan<T> colors)
|
||||||
where T : struct, IColor
|
where T : struct, IColor
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user