Added special case for small amounts of data for all copying helpers

This commit is contained in:
Darth Affe 2024-07-21 17:42:01 +02:00
parent cfdd95f079
commit bd7a2bca48
4 changed files with 210 additions and 69 deletions

View File

@ -13,6 +13,16 @@ public static partial class PixelHelper
ArgumentNullException.ThrowIfNull(image);
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);
Span<byte> buffer = array.AsSpan()[..dataLength];
try
@ -25,6 +35,7 @@ public static partial class PixelHelper
ArrayPool<byte>.Shared.Return(array);
}
}
}
public static T Average<T>(this IImage<T> image)
where T : struct, IColor
@ -34,8 +45,19 @@ public static partial class PixelHelper
where T : struct, IColor
{
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);
Span<T> buffer = array.AsSpan()[..(dataLength)];
Span<T> buffer = array.AsSpan()[..dataLength];
try
{
image.CopyTo(buffer);
@ -46,6 +68,7 @@ public static partial class PixelHelper
ArrayPool<T>.Shared.Return(array);
}
}
}
public static T Average<T>(this Span<T> colors)
where T : struct, IColor

View File

@ -14,6 +14,16 @@ public static unsafe partial class PixelHelper
ArgumentNullException.ThrowIfNull(image);
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);
Span<byte> buffer = array.AsSpan()[..dataLength];
try
@ -26,6 +36,7 @@ public static unsafe partial class PixelHelper
ArrayPool<byte>.Shared.Return(array);
}
}
}
public static IMinMax MinMax<T>(this IImage<T> image)
where T : struct, IColor
@ -35,6 +46,17 @@ public static unsafe partial class PixelHelper
where T : struct, IColor
{
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);
Span<T> buffer = array.AsSpan()[..(dataLength)];
try
@ -47,6 +69,7 @@ public static unsafe partial class PixelHelper
ArrayPool<T>.Shared.Return(array);
}
}
}
public static IMinMax MinMax<T>(this Span<T> colors)
where T : struct, IColor
=> T.ColorFormat.MinMax(MemoryMarshal.AsBytes(colors));

View File

@ -1,5 +1,7 @@
using System.Buffers;
using System.Numerics;
using System.Runtime.InteropServices;
using static System.Net.Mime.MediaTypeNames;
namespace HPPH;
@ -12,6 +14,16 @@ public static partial class PixelHelper
ArgumentNullException.ThrowIfNull(image);
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);
Span<byte> buffer = array.AsSpan()[..dataLength];
try
@ -24,6 +36,7 @@ public static partial class PixelHelper
ArrayPool<byte>.Shared.Return(array);
}
}
}
public static T[] CreateColorPalette<T>(this IImage<T> image, int paletteSize)
where T : unmanaged, IColor
@ -33,6 +46,17 @@ public static partial class PixelHelper
where T : unmanaged, IColor
{
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);
Span<T> buffer = array.AsSpan()[..(dataLength)];
try
@ -45,9 +69,21 @@ public static partial class PixelHelper
ArrayPool<T>.Shared.Return(array);
}
}
}
public static T[] CreateColorPalette<T>(this ReadOnlySpan<T> colors, int paletteSize)
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);
try
@ -62,6 +98,7 @@ public static partial class PixelHelper
ArrayPool<T>.Shared.Return(buffer);
}
}
}
public static T[] CreateColorPalette<T>(this Span<T> colors, int paletteSize)
where T : unmanaged, IColor
@ -102,6 +139,16 @@ public static partial class PixelHelper
ArgumentNullException.ThrowIfNull(image);
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);
Span<byte> buffer = array.AsSpan()[..dataLength];
try
@ -114,6 +161,7 @@ public static partial class PixelHelper
ArrayPool<byte>.Shared.Return(array);
}
}
}
public static T[] CreateSimpleColorPalette<T>(this IImage<T> image, int paletteSize)
where T : unmanaged, IColor
@ -123,6 +171,17 @@ public static partial class PixelHelper
where T : unmanaged, IColor
{
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);
Span<T> buffer = array.AsSpan()[..(dataLength)];
try
@ -135,9 +194,21 @@ public static partial class PixelHelper
ArrayPool<T>.Shared.Return(array);
}
}
}
public static T[] CreateSimpleColorPalette<T>(this ReadOnlySpan<T> colors, int paletteSize)
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);
try
@ -152,6 +223,7 @@ public static partial class PixelHelper
ArrayPool<T>.Shared.Return(buffer);
}
}
}
public static unsafe T[] CreateSimpleColorPalette<T>(this Span<T> colors, int paletteSize)
where T : unmanaged, IColor

View File

@ -15,6 +15,16 @@ public static unsafe partial class PixelHelper
ArgumentNullException.ThrowIfNull(image);
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);
Span<byte> buffer = array.AsSpan()[..dataLength];
try
@ -27,6 +37,7 @@ public static unsafe partial class PixelHelper
ArrayPool<byte>.Shared.Return(array);
}
}
}
public static ISum Sum<T>(this IImage<T> image)
where T : struct, IColor
@ -36,6 +47,17 @@ public static unsafe partial class PixelHelper
where T : struct, IColor
{
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);
Span<T> buffer = array.AsSpan()[..(dataLength)];
try
@ -48,6 +70,7 @@ public static unsafe partial class PixelHelper
ArrayPool<T>.Shared.Return(array);
}
}
}
public static ISum Sum<T>(this ReadOnlySpan<T> colors)
where T : struct, IColor