Compare commits

...

7 Commits

12 changed files with 226 additions and 49 deletions

View File

@ -0,0 +1,58 @@
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using HPPH.System.Drawing;
namespace HPPH.Benchmark;
[SimpleJob(RuntimeMoniker.Net80)]
[HtmlExporter]
[MemoryDiagnoser]
public class ConvertBenchmarks
{
#region Properties & Fields
private readonly List<ColorRGB[]> _colors = [];
#endregion
#region Constructors
public ConvertBenchmarks()
{
if (!Directory.Exists(@"..\..\..\..\sample_data")) return;
_colors = [];
IEnumerable<string> files = Directory.EnumerateFiles(@"..\..\..\..\sample_data", "*.png", SearchOption.AllDirectories);
foreach (string file in files)
_colors.Add(ImageHelper.LoadImage(file).AsRefImage<ColorRGB>().ToArray());
}
#endregion
#region Methods
[Benchmark]
public ColorBGR[] RGBToBGR()
{
ColorBGR[] result = [];
foreach (ColorRGB[] color in _colors)
{
result = new ReadOnlySpan<ColorRGB>(color).Convert<ColorRGB, ColorBGR>();
}
return result;
}
[Benchmark]
public ColorBGRA[] RGBToBGRA()
{
ColorBGRA[] result = [];
foreach (ColorRGB[] color in _colors)
result = new ReadOnlySpan<ColorRGB>(color).Convert<ColorRGB, ColorBGRA>();
return result;
}
#endregion
}

View File

@ -13,6 +13,7 @@
<ItemGroup>
<ProjectReference Include="..\HPPH.Reference\HPPH.Reference.csproj" />
<ProjectReference Include="..\HPPH.System.Drawing\HPPH.System.Drawing.csproj" />
<ProjectReference Include="..\HPPH\HPPH.csproj" />
</ItemGroup>

View File

@ -1 +1,4 @@
Console.WriteLine("Empty");
using BenchmarkDotNet.Running;
using HPPH.Benchmark;
BenchmarkRunner.Run<ConvertBenchmarks>();

View File

@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Drawing.Common" Version="8.0.6" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\HPPH\HPPH.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,32 @@
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.Versioning;
namespace HPPH.System.Drawing;
public static class ImageExtension
{
public static Bitmap ToBitmap(this IImage image)
{
throw new NotImplementedException();
}
[SupportedOSPlatform("windows")]
public static unsafe IImage ToImage(this Bitmap bitmap)
{
BitmapData data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, bitmap.PixelFormat);
ReadOnlySpan<byte> buffer = new(data.Scan0.ToPointer(), data.Stride * data.Height);
IImage image;
if (data.PixelFormat.HasFlag(PixelFormat.Format24bppRgb))
image = Image<ColorRGB>.Create(buffer, data.Width, data.Height, data.Stride);
else if (data.PixelFormat.HasFlag(PixelFormat.Format32bppArgb))
image = Image<ColorARGB>.Create(buffer, data.Width, data.Height, data.Stride);
else throw new NotSupportedException($"Unsupported pixel format '{bitmap.PixelFormat}'.");
bitmap.UnlockBits(data);
return image;
}
}

View File

@ -0,0 +1,21 @@
using System.Drawing;
using System.Runtime.Versioning;
namespace HPPH.System.Drawing;
public static class ImageHelper
{
[SupportedOSPlatform("windows")]
public static IImage LoadImage(string path)
{
using Bitmap bmp = new(path);
return bmp.ToImage();
}
[SupportedOSPlatform("windows")]
public static IImage LoadImage(Stream stream)
{
using Bitmap bmp = new(stream);
return bmp.ToImage();
}
}

View File

@ -11,7 +11,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HPPH.Benchmark", "HPPH.Benc
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HPPH.Reference", "HPPH.Reference\HPPH.Reference.csproj", "{1675FE68-1F51-4202-9567-BB46215B2BBD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HPPH.Generators", "HPPH.Generators\HPPH.Generators.csproj", "{C247512B-E6D2-4591-8AFA-F2268F1AEAB2}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HPPH.Generators", "HPPH.Generators\HPPH.Generators.csproj", "{C247512B-E6D2-4591-8AFA-F2268F1AEAB2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HPPH.System.Drawing", "HPPH.System.Drawing\HPPH.System.Drawing.csproj", "{16EC37E4-3EF1-47AF-B257-465334B36571}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -39,6 +41,10 @@ Global
{C247512B-E6D2-4591-8AFA-F2268F1AEAB2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C247512B-E6D2-4591-8AFA-F2268F1AEAB2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C247512B-E6D2-4591-8AFA-F2268F1AEAB2}.Release|Any CPU.Build.0 = Release|Any CPU
{16EC37E4-3EF1-47AF-B257-465334B36571}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{16EC37E4-3EF1-47AF-B257-465334B36571}.Debug|Any CPU.Build.0 = Debug|Any CPU
{16EC37E4-3EF1-47AF-B257-465334B36571}.Release|Any CPU.ActiveCfg = Release|Any CPU
{16EC37E4-3EF1-47AF-B257-465334B36571}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -45,7 +45,7 @@ public sealed class Image<TColor> : IImage
{
if ((x < 0) || (y < 0) || (x >= Width) || (y >= Height)) throw new IndexOutOfRangeException();
return MemoryMarshal.Cast<byte, TColor>(_buffer)[((_y + y) * _stride) + (_x + x)];
return MemoryMarshal.Cast<byte, TColor>(_buffer.AsSpan()[((_y + y) * _stride)..])[_x + x];
}
}
@ -79,7 +79,7 @@ public sealed class Image<TColor> : IImage
#region Constructors
internal Image(byte[] buffer, int x, int y, int width, int height, int stride)
private Image(byte[] buffer, int x, int y, int width, int height, int stride)
{
this._buffer = buffer;
this._x = x;
@ -93,6 +93,19 @@ public sealed class Image<TColor> : IImage
#region Methods
public static Image<TColor> Create(ReadOnlySpan<TColor> buffer, int width, int height)
=> Create(MemoryMarshal.AsBytes(buffer), width, height, width * TColor.ColorFormat.BytesPerPixel);
public static Image<TColor> Create(ReadOnlySpan<byte> buffer, int width, int height, int stride)
{
if (stride < width) throw new ArgumentException("Stride can't be smaller than width.");
if (buffer.Length < (height * stride)) throw new ArgumentException("Not enough data in the buffer.");
byte[] data = new byte[buffer.Length];
buffer.CopyTo(data);
return new Image<TColor>(data, 0, 0, width, height, stride);
}
/// <inheritdoc />
public void CopyTo(Span<byte> destination)
{
@ -136,7 +149,7 @@ public sealed class Image<TColor> : IImage
{
if (typeof(T) != typeof(TColor)) throw new ArgumentException("The requested color format does not fit this image.", nameof(T));
return new RefImage<T>(MemoryMarshal.Cast<byte, T>(_buffer), _x, _y, Width, Height, _stride);
return new RefImage<T>(_buffer, _x, _y, Width, Height, _stride);
}
/// <inheritdoc />
@ -180,7 +193,7 @@ public sealed class Image<TColor> : IImage
{
if ((row < 0) || (row >= _height)) throw new IndexOutOfRangeException();
return new ImageRow(_buffer, (((row + _y) * _stride) + _x), _width);
return new ImageRow(_buffer, ((row + _y) * _stride) + (_x * TColor.ColorFormat.BytesPerPixel), _width);
}
}
@ -241,8 +254,7 @@ public sealed class Image<TColor> : IImage
{
if ((x < 0) || (x >= _length)) throw new IndexOutOfRangeException();
ReadOnlySpan<TColor> row = MemoryMarshal.Cast<byte, TColor>(_buffer)[_start..];
return row[x];
return MemoryMarshal.Cast<byte, TColor>(_buffer)[_start..][x];
}
}
@ -267,7 +279,7 @@ public sealed class Image<TColor> : IImage
if (destination == null) throw new ArgumentNullException(nameof(destination));
if (destination.Length < SizeInBytes) throw new ArgumentException("The destination is too small to fit this image.", nameof(destination));
MemoryMarshal.Cast<byte, TColor>(_buffer).Slice(_start, _length).CopyTo(MemoryMarshal.Cast<byte, TColor>(destination));
_buffer.AsSpan().Slice(_start, SizeInBytes).CopyTo(destination);
}
/// <inheritdoc />
@ -317,7 +329,7 @@ public sealed class Image<TColor> : IImage
{
if ((column < 0) || (column >= _width)) throw new IndexOutOfRangeException();
return new ImageColumn(_buffer, (_y * _stride) + _x + column, _height, _stride);
return new ImageColumn(_buffer, (_y * _stride) + ((_x + column) * TColor.ColorFormat.BytesPerPixel), _height, _stride);
}
}
@ -379,8 +391,7 @@ public sealed class Image<TColor> : IImage
{
if ((y < 0) || (y >= _length)) throw new IndexOutOfRangeException();
ReadOnlySpan<TColor> data = MemoryMarshal.Cast<byte, TColor>(_buffer)[_start..];
return data[y * _step];
return MemoryMarshal.Cast<byte, TColor>(_buffer.AsSpan()[_start..])[y * _step];
}
}
@ -410,7 +421,7 @@ public sealed class Image<TColor> : IImage
_buffer.AsSpan(_start, SizeInBytes).CopyTo(destination);
else
{
ReadOnlySpan<TColor> data = MemoryMarshal.Cast<byte, TColor>(_buffer)[_start..];
ReadOnlySpan<TColor> data = MemoryMarshal.Cast<byte, TColor>(_buffer.AsSpan()[_start..]);
Span<TColor> target = MemoryMarshal.Cast<byte, TColor>(destination);
for (int i = 0; i < Length; i++)
target[i] = data[i * _step];

View File

@ -8,7 +8,7 @@ public readonly ref struct RefImage<TColor>
{
#region Properties & Fields
private readonly ReadOnlySpan<TColor> _pixels;
private readonly ReadOnlySpan<byte> _data;
private readonly int _x;
private readonly int _y;
@ -40,8 +40,9 @@ public readonly ref struct RefImage<TColor>
{
if ((x < 0) || (y < 0) || (x >= Width) || (y >= Height)) throw new IndexOutOfRangeException();
ref TColor r0 = ref MemoryMarshal.GetReference(_pixels);
nint offset = (nint)(uint)((_y + y) * RawStride) + (_x + x);
ReadOnlySpan<TColor> data = MemoryMarshal.Cast<byte, TColor>(_data[((_y + y) * RawStride)..]);
ref TColor r0 = ref MemoryMarshal.GetReference(data);
nint offset = (nint)(uint)(_x + x);
return ref Unsafe.Add(ref r0, offset);
}
}
@ -53,29 +54,29 @@ public readonly ref struct RefImage<TColor>
{
if ((x < 0) || (y < 0) || (width <= 0) || (height <= 0) || ((x + width) > Width) || ((y + height) > Height)) throw new IndexOutOfRangeException();
return new RefImage<TColor>(_pixels, _x + x, _y + y, width, height, RawStride);
return new RefImage<TColor>(_data, _x + x, _y + y, width, height, RawStride);
}
}
public ImageRows Rows
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => new(_pixels, _x, _y, Width, Height, RawStride);
get => new(_data, _x, _y, Width, Height, RawStride);
}
public ImageColumns Columns
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => new(_pixels, _x, _y, Width, Height, RawStride);
get => new(_data, _x, _y, Width, Height, RawStride);
}
#endregion
#region Constructors
internal RefImage(ReadOnlySpan<TColor> pixels, int x, int y, int width, int height, int stride)
internal RefImage(ReadOnlySpan<byte> data, int x, int y, int width, int height, int stride)
{
this._pixels = pixels;
this._data = data;
this._x = x;
this._y = y;
this.Width = width;
@ -122,44 +123,63 @@ public readonly ref struct RefImage<TColor>
/// <summary>
/// Returns a reference to the first element of this image inside the full image buffer.
/// </summary>
public ref readonly TColor GetPinnableReference()
public ref readonly byte GetPinnableReference()
{
if (_pixels.Length == 0)
return ref Unsafe.NullRef<TColor>();
if (_data.Length == 0)
return ref Unsafe.NullRef<byte>();
int offset = (_y * RawStride) + _x;
return ref MemoryMarshal.GetReference(_pixels[offset..]);
int offset = (_y * RawStride) + (_x * TColor.ColorFormat.BytesPerPixel);
return ref MemoryMarshal.GetReference(_data[offset..]);
}
/// <inheritdoc cref="System.Collections.IEnumerable.GetEnumerator"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ImageEnumerator GetEnumerator() => new(_pixels);
public ImageEnumerator GetEnumerator() => new(_data, _x, _y, Width, Height, RawStride);
#endregion
//TODO DarthAffe 10.07.2024: anpassen
public ref struct ImageEnumerator
{
#region Properties & Fields
private readonly ReadOnlySpan<TColor> _pixels;
private readonly ReadOnlySpan<byte> _data;
private readonly int _x;
private readonly int _y;
private readonly int _width;
private readonly int _stride;
private readonly int _count;
private int _position;
/// <inheritdoc cref="System.Collections.Generic.IEnumerator{T}.Current"/>
public readonly TColor Current
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _pixels[_position];
get
{
int row = (_position / _width);
int column = _position - (row * _width);
return MemoryMarshal.Cast<byte, TColor>(_data[(_y * _stride)..])[_x + column];
}
}
#endregion
#region Constructors
// ReSharper disable once ConvertToPrimaryConstructor - Not possible with ref types
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal ImageEnumerator(ReadOnlySpan<TColor> pixels)
internal ImageEnumerator(ReadOnlySpan<byte> data, int x, int y, int width, int height, int stride)
{
this._pixels = pixels;
this._data = data;
this._x = x;
this._y = y;
this._width = width;
this._stride = stride;
_count = _width * height;
_position = -1;
}
@ -170,7 +190,7 @@ public readonly ref struct RefImage<TColor>
/// <inheritdoc cref="System.Collections.IEnumerator.MoveNext"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool MoveNext() => ++_position < _pixels.Length;
public bool MoveNext() => ++_position < _count;
#endregion
}
@ -181,7 +201,7 @@ public readonly ref struct RefImage<TColor>
{
#region Properties & Fields
private readonly ReadOnlySpan<TColor> _pixels;
private readonly ReadOnlySpan<byte> _data;
private readonly int _x;
private readonly int _y;
private readonly int _width;
@ -201,8 +221,10 @@ public readonly ref struct RefImage<TColor>
{
if ((row < 0) || (row > _height)) throw new IndexOutOfRangeException();
ref TColor r0 = ref MemoryMarshal.GetReference(_pixels);
ref TColor rr = ref Unsafe.Add(ref r0, (nint)(uint)(((row + _y) * _stride) + _x));
ReadOnlySpan<TColor> data = MemoryMarshal.Cast<byte, TColor>(_data[((row + _y) * _stride)..]);
ref TColor r0 = ref MemoryMarshal.GetReference(data);
ref TColor rr = ref Unsafe.Add(ref r0, (nint)(uint)_x);
return new ReadOnlyRefEnumerable<TColor>(rr, _width, 1);
}
@ -213,9 +235,9 @@ public readonly ref struct RefImage<TColor>
#region Constructors
// ReSharper disable once ConvertToPrimaryConstructor - Not possible with ref types
public ImageRows(ReadOnlySpan<TColor> pixels, int x, int y, int width, int height, int stride)
internal ImageRows(ReadOnlySpan<byte> data, int x, int y, int width, int height, int stride)
{
this._pixels = pixels;
this._data = data;
this._x = x;
this._y = y;
this._width = width;
@ -276,7 +298,7 @@ public readonly ref struct RefImage<TColor>
{
#region Properties & Fields
private readonly ReadOnlySpan<TColor> _pixels;
private readonly ReadOnlySpan<byte> _data;
private readonly int _x;
private readonly int _y;
private readonly int _width;
@ -296,8 +318,9 @@ public readonly ref struct RefImage<TColor>
{
if ((column < 0) || (column > _width)) throw new IndexOutOfRangeException();
ref TColor r0 = ref MemoryMarshal.GetReference(_pixels);
ref TColor rc = ref Unsafe.Add(ref r0, (nint)(uint)((_y * _stride) + (column + _x)));
ReadOnlySpan<TColor> data = MemoryMarshal.Cast<byte, TColor>(_data[(_y * _stride)..]);
ref TColor r0 = ref MemoryMarshal.GetReference(data);
ref TColor rc = ref Unsafe.Add(ref r0, (nint)(uint)(column + _x));
return new ReadOnlyRefEnumerable<TColor>(rc, _height, _stride);
}
@ -308,9 +331,9 @@ public readonly ref struct RefImage<TColor>
#region Constructors
// ReSharper disable once ConvertToPrimaryConstructor - Not possible with ref types
public ImageColumns(ReadOnlySpan<TColor> pixels, int x, int y, int width, int height, int stride)
internal ImageColumns(ReadOnlySpan<byte> data, int x, int y, int width, int height, int stride)
{
this._pixels = pixels;
this._data = data;
this._x = x;
this._y = y;
this._width = width;

View File

@ -43,6 +43,9 @@ 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));
public static IMinMax MinMax<T>(this ReadOnlySpan<T> colors)
where T : struct, IColor

View File

@ -211,11 +211,12 @@ public static unsafe partial class PixelHelper
4, 0
];
Vector256<int> controlVector = Vector256.LoadUnsafe(ref MemoryMarshal.GetReference(avx2ControlData));
Vector256<long> rgbaSum64 = Vector256<long>.Zero;
ReadOnlySpan<byte> dataBytes = MemoryMarshal.AsBytes(data);
fixed (byte* bytePtr = dataBytes)
fixed (int* controlPtr = avx2ControlData)
fixed (byte* maskPtr = avx2ShuffleMask)
{
Vector256<byte> avx2ShuffleMaskVector = Avx2.BroadcastVector128ToVector256(maskPtr);
@ -224,7 +225,7 @@ public static unsafe partial class PixelHelper
{
Vector256<byte> chunk = Vector256.Load(bytePtr + (i * 4));
Vector256<byte> deinterleaved = Avx2.Shuffle(chunk, avx2ShuffleMaskVector);
Vector256<int> deinterleaved2 = Avx2.PermuteVar8x32(deinterleaved.AsInt32(), Vector256.Load(controlPtr));
Vector256<int> deinterleaved2 = Avx2.PermuteVar8x32(deinterleaved.AsInt32(), controlVector);
Vector256<long> sum = Avx2.SumAbsoluteDifferences(deinterleaved2.AsByte(), Vector256<byte>.Zero).AsInt64();
rgbaSum64 = Avx2.Add(rgbaSum64, sum);
}

View File

@ -43,26 +43,26 @@ internal struct ColorCube<T>
private void OrderColors(Span<T> colors, SortTarget preOrdered)
{
if (colors.Length < 2) return;
IMinMax colorRanges = PixelHelper.MinMax<T>(colors);
IMinMax colorRanges = colors.MinMax();
if ((colorRanges.RedRange > colorRanges.GreenRange) && (colorRanges.RedRange > colorRanges.BlueRange))
{
if (preOrdered != SortTarget.Red)
PixelHelper.SortByRed(colors);
colors.SortByRed();
_sortOrder = SortTarget.Red;
}
else if (colorRanges.GreenRange > colorRanges.BlueRange)
{
if (preOrdered != SortTarget.Green)
PixelHelper.SortByGreen(colors);
colors.SortByGreen();
_sortOrder = SortTarget.Green;
}
else
{
if (preOrdered != SortTarget.Blue)
PixelHelper.SortByBlue(colors);
colors.SortByBlue();
_sortOrder = SortTarget.Blue;
}