Fixed a lot of isssues brought up by the analyzers

This commit is contained in:
Darth Affe 2024-08-11 20:11:46 +02:00
parent cddb82e07e
commit b66fc3c9e9
24 changed files with 230 additions and 36 deletions

7
Analyzers.globalconfig Normal file
View File

@ -0,0 +1,7 @@
is_global = true
# CA1000: Do not declare static members on generic types
dotnet_diagnostic.CA1000.severity = none
# CA1034: Nested types should not be visible
dotnet_diagnostic.CA1034.severity = none

View File

@ -1,4 +1,6 @@
using HPPH.System.Drawing;
#pragma warning disable CA1416
using HPPH.System.Drawing;
namespace HPPH.Benchmark;

View File

@ -49,7 +49,7 @@ internal class Colors : IGeneratorFeature
[DebuggerDisplay("[A: {A}, R: {R}, G: {G}, B: {B}]")]
[SkipLocalsInit]
[StructLayout(LayoutKind.Sequential)]
public readonly partial struct {{colorFormat.TypeName}}(byte {{colorFormat.FirstEntry}}, byte {{colorFormat.SecondEntry}}, byte {{colorFormat.ThirdEntry}}): IColor
public readonly partial struct {{colorFormat.TypeName}}(byte {{colorFormat.FirstEntry}}, byte {{colorFormat.SecondEntry}}, byte {{colorFormat.ThirdEntry}}): IColor, IEquatable<{{colorFormat.TypeName}}>
{
#region Properties & Fields
@ -74,11 +74,27 @@ internal class Colors : IGeneratorFeature
#endregion
#region Operators
public static bool operator ==({{colorFormat.TypeName}} left, {{colorFormat.TypeName}} right) => left.Equals(right);
public static bool operator !=({{colorFormat.TypeName}} left, {{colorFormat.TypeName}} right) => !left.Equals(right);
#endregion
#region Methods
/// <inheritdoc />
public bool Equals(IColor? other) => (other != null) && (R == other.R) && (G == other.G) && (B == other.B) && (A == other.A);
/// <inheritdoc />
public bool Equals({{colorFormat.TypeName}} other) => (_{{colorFormat.FirstEntry}} == other._{{colorFormat.FirstEntry}}) && (_{{colorFormat.SecondEntry}} == other._{{colorFormat.SecondEntry}}) && (_{{colorFormat.ThirdEntry}} == other._{{colorFormat.ThirdEntry}});
/// <inheritdoc />
public override bool Equals(object? obj) => obj is {{colorFormat.TypeName}} other && Equals(other);
/// <inheritdoc />
public override int GetHashCode() => HashCode.Combine(_{{colorFormat.FirstEntry}}, _{{colorFormat.SecondEntry}}, _{{colorFormat.ThirdEntry}});
/// <inheritdoc />
public override string ToString() => $"[A: {A}, R: {R}, G: {G}, B: {B}]";
@ -116,7 +132,7 @@ internal class Colors : IGeneratorFeature
[DebuggerDisplay("[A: {A}, R: {R}, G: {G}, B: {B}]")]
[SkipLocalsInit]
[StructLayout(LayoutKind.Sequential)]
public readonly partial struct {{colorFormat.TypeName}}(byte {{colorFormat.FirstEntry}}, byte {{colorFormat.SecondEntry}}, byte {{colorFormat.ThirdEntry}}, byte {{colorFormat.FourthEntry}}) : IColor
public readonly partial struct {{colorFormat.TypeName}}(byte {{colorFormat.FirstEntry}}, byte {{colorFormat.SecondEntry}}, byte {{colorFormat.ThirdEntry}}, byte {{colorFormat.FourthEntry}}) : IColor, IEquatable<{{colorFormat.TypeName}}>
{
#region Properties & Fields
@ -142,11 +158,27 @@ internal class Colors : IGeneratorFeature
#endregion
#region Operators
public static bool operator ==({{colorFormat.TypeName}} left, {{colorFormat.TypeName}} right) => left.Equals(right);
public static bool operator !=({{colorFormat.TypeName}} left, {{colorFormat.TypeName}} right) => !left.Equals(right);
#endregion
#region Methods
/// <inheritdoc />
public bool Equals(IColor? other) => (other != null) && (R == other.R) && (G == other.G) && (B == other.B) && (A == other.A);
/// <inheritdoc />
public bool Equals({{colorFormat.TypeName}} other) => (_{{colorFormat.FirstEntry}} == other._{{colorFormat.FirstEntry}}) && (_{{colorFormat.SecondEntry}} == other._{{colorFormat.SecondEntry}}) && (_{{colorFormat.ThirdEntry}} == other._{{colorFormat.ThirdEntry}})&& (_{{colorFormat.FourthEntry}}== other._{{colorFormat.FourthEntry}});
/// <inheritdoc />
public override bool Equals(object? obj) => obj is {{colorFormat.TypeName}} other && Equals(other);
/// <inheritdoc />
public override int GetHashCode() => HashCode.Combine(_{{colorFormat.FirstEntry}}, _{{colorFormat.SecondEntry}}, _{{colorFormat.ThirdEntry}}, _{{colorFormat.FourthEntry}});
/// <inheritdoc />
public override string ToString() => $"[A: {A}, R: {R}, G: {G}, B: {B}]";

View File

@ -44,6 +44,10 @@
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
</PropertyGroup>
<ItemGroup>
<GlobalAnalyzerConfigFiles Include="../Analyzers.globalconfig" />
</ItemGroup>
<PropertyGroup Condition="'$(Configuration)'=='Debug'">
<DefineConstants>$(DefineConstants);TRACE;DEBUG</DefineConstants>
<DebugSymbols>true</DebugSymbols>

View File

@ -1,17 +1,24 @@
// ReSharper disable InconsistentNaming
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using SkiaSharp;
namespace HPPH.SkiaSharp;
public static class ImageExtension
{
public static unsafe SKImage ToSKImage(this IImage image) => SKImage.FromBitmap(image.ToSKBitmap());
public static unsafe SKImage ToSKImage(this IImage image)
{
ArgumentNullException.ThrowIfNull(image, nameof(image));
using SKBitmap bitmap = image.ToSKBitmap();
return SKImage.FromBitmap(bitmap);
}
public static unsafe SKBitmap ToSKBitmap(this IImage image)
{
ArgumentNullException.ThrowIfNull(image, nameof(image));
SKBitmap bitmap = new(image.Width, image.Height, SKColorType.Bgra8888, SKAlphaType.Unpremul);
nint pixelPtr = bitmap.GetPixels(out nint length);
image.ConvertTo<ColorBGRA>().CopyTo(new Span<byte>((void*)pixelPtr, (int)length));
@ -28,5 +35,9 @@ public static class ImageExtension
public static IImage ToImage(this SKImage skImage) => SKBitmap.FromImage(skImage).ToImage();
public static IImage ToImage(this SKBitmap bitmap)
=> Image<ColorBGRA>.Create(MemoryMarshal.Cast<SKColor, ColorBGRA>(bitmap.Pixels), bitmap.Width, bitmap.Height);
{
ArgumentNullException.ThrowIfNull(bitmap, nameof(bitmap));
return Image<ColorBGRA>.Create(MemoryMarshal.Cast<SKColor, ColorBGRA>(bitmap.Pixels), bitmap.Width, bitmap.Height);
}
}

View File

@ -44,6 +44,10 @@
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
</PropertyGroup>
<ItemGroup>
<GlobalAnalyzerConfigFiles Include="../Analyzers.globalconfig" />
</ItemGroup>
<PropertyGroup Condition="'$(Configuration)'=='Debug'">
<DefineConstants>$(DefineConstants);TRACE;DEBUG</DefineConstants>
<DebugSymbols>true</DebugSymbols>

View File

@ -9,6 +9,8 @@ public static class ImageExtension
[SupportedOSPlatform("windows")]
public static unsafe Bitmap ToBitmap(this IImage image)
{
ArgumentNullException.ThrowIfNull(image, nameof(image));
switch (image.ColorFormat.BytesPerPixel)
{
case 3:
@ -57,6 +59,8 @@ public static class ImageExtension
[SupportedOSPlatform("windows")]
public static byte[] ToPng(this IImage image)
{
ArgumentNullException.ThrowIfNull(image, nameof(image));
using Bitmap bitmap = ToBitmap(image);
using MemoryStream ms = new();
bitmap.Save(ms, ImageFormat.Png);
@ -67,6 +71,8 @@ public static class ImageExtension
[SupportedOSPlatform("windows")]
public static unsafe IImage ToImage(this Bitmap bitmap)
{
ArgumentNullException.ThrowIfNull(bitmap, nameof(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);

View File

@ -23,7 +23,7 @@ namespace HPPH;
[DebuggerDisplay("[A: {A}, R: {R}, G: {G}, B: {B}]")]
[SkipLocalsInit]
[StructLayout(LayoutKind.Sequential)]
public readonly partial struct ColorABGR(byte a, byte b, byte g, byte r) : IColor
public readonly partial struct ColorABGR(byte a, byte b, byte g, byte r) : IColor, IEquatable<ColorABGR>
{
#region Properties & Fields
@ -49,11 +49,27 @@ public readonly partial struct ColorABGR(byte a, byte b, byte g, byte r) : IColo
#endregion
#region Operators
public static bool operator ==(ColorABGR left, ColorABGR right) => left.Equals(right);
public static bool operator !=(ColorABGR left, ColorABGR right) => !left.Equals(right);
#endregion
#region Methods
/// <inheritdoc />
public bool Equals(IColor? other) => (other != null) && (R == other.R) && (G == other.G) && (B == other.B) && (A == other.A);
/// <inheritdoc />
public bool Equals(ColorABGR other) => (_a == other._a) && (_b == other._b) && (_g == other._g)&& (_r== other._r);
/// <inheritdoc />
public override bool Equals(object? obj) => obj is ColorABGR other && Equals(other);
/// <inheritdoc />
public override int GetHashCode() => HashCode.Combine(_a, _b, _g, _r);
/// <inheritdoc />
public override string ToString() => $"[A: {A}, R: {R}, G: {G}, B: {B}]";

View File

@ -23,7 +23,7 @@ namespace HPPH;
[DebuggerDisplay("[A: {A}, R: {R}, G: {G}, B: {B}]")]
[SkipLocalsInit]
[StructLayout(LayoutKind.Sequential)]
public readonly partial struct ColorARGB(byte a, byte r, byte g, byte b) : IColor
public readonly partial struct ColorARGB(byte a, byte r, byte g, byte b) : IColor, IEquatable<ColorARGB>
{
#region Properties & Fields
@ -49,11 +49,27 @@ public readonly partial struct ColorARGB(byte a, byte r, byte g, byte b) : IColo
#endregion
#region Operators
public static bool operator ==(ColorARGB left, ColorARGB right) => left.Equals(right);
public static bool operator !=(ColorARGB left, ColorARGB right) => !left.Equals(right);
#endregion
#region Methods
/// <inheritdoc />
public bool Equals(IColor? other) => (other != null) && (R == other.R) && (G == other.G) && (B == other.B) && (A == other.A);
/// <inheritdoc />
public bool Equals(ColorARGB other) => (_a == other._a) && (_r == other._r) && (_g == other._g)&& (_b== other._b);
/// <inheritdoc />
public override bool Equals(object? obj) => obj is ColorARGB other && Equals(other);
/// <inheritdoc />
public override int GetHashCode() => HashCode.Combine(_a, _r, _g, _b);
/// <inheritdoc />
public override string ToString() => $"[A: {A}, R: {R}, G: {G}, B: {B}]";

View File

@ -22,7 +22,7 @@ namespace HPPH;
[DebuggerDisplay("[A: {A}, R: {R}, G: {G}, B: {B}]")]
[SkipLocalsInit]
[StructLayout(LayoutKind.Sequential)]
public readonly partial struct ColorBGR(byte b, byte g, byte r): IColor
public readonly partial struct ColorBGR(byte b, byte g, byte r): IColor, IEquatable<ColorBGR>
{
#region Properties & Fields
@ -47,11 +47,27 @@ public readonly partial struct ColorBGR(byte b, byte g, byte r): IColor
#endregion
#region Operators
public static bool operator ==(ColorBGR left, ColorBGR right) => left.Equals(right);
public static bool operator !=(ColorBGR left, ColorBGR right) => !left.Equals(right);
#endregion
#region Methods
/// <inheritdoc />
public bool Equals(IColor? other) => (other != null) && (R == other.R) && (G == other.G) && (B == other.B) && (A == other.A);
/// <inheritdoc />
public bool Equals(ColorBGR other) => (_b == other._b) && (_g == other._g) && (_r == other._r);
/// <inheritdoc />
public override bool Equals(object? obj) => obj is ColorBGR other && Equals(other);
/// <inheritdoc />
public override int GetHashCode() => HashCode.Combine(_b, _g, _r);
/// <inheritdoc />
public override string ToString() => $"[A: {A}, R: {R}, G: {G}, B: {B}]";

View File

@ -23,7 +23,7 @@ namespace HPPH;
[DebuggerDisplay("[A: {A}, R: {R}, G: {G}, B: {B}]")]
[SkipLocalsInit]
[StructLayout(LayoutKind.Sequential)]
public readonly partial struct ColorBGRA(byte b, byte g, byte r, byte a) : IColor
public readonly partial struct ColorBGRA(byte b, byte g, byte r, byte a) : IColor, IEquatable<ColorBGRA>
{
#region Properties & Fields
@ -49,11 +49,27 @@ public readonly partial struct ColorBGRA(byte b, byte g, byte r, byte a) : IColo
#endregion
#region Operators
public static bool operator ==(ColorBGRA left, ColorBGRA right) => left.Equals(right);
public static bool operator !=(ColorBGRA left, ColorBGRA right) => !left.Equals(right);
#endregion
#region Methods
/// <inheritdoc />
public bool Equals(IColor? other) => (other != null) && (R == other.R) && (G == other.G) && (B == other.B) && (A == other.A);
/// <inheritdoc />
public bool Equals(ColorBGRA other) => (_b == other._b) && (_g == other._g) && (_r == other._r)&& (_a== other._a);
/// <inheritdoc />
public override bool Equals(object? obj) => obj is ColorBGRA other && Equals(other);
/// <inheritdoc />
public override int GetHashCode() => HashCode.Combine(_b, _g, _r, _a);
/// <inheritdoc />
public override string ToString() => $"[A: {A}, R: {R}, G: {G}, B: {B}]";

View File

@ -22,7 +22,7 @@ namespace HPPH;
[DebuggerDisplay("[A: {A}, R: {R}, G: {G}, B: {B}]")]
[SkipLocalsInit]
[StructLayout(LayoutKind.Sequential)]
public readonly partial struct ColorRGB(byte r, byte g, byte b): IColor
public readonly partial struct ColorRGB(byte r, byte g, byte b): IColor, IEquatable<ColorRGB>
{
#region Properties & Fields
@ -47,11 +47,27 @@ public readonly partial struct ColorRGB(byte r, byte g, byte b): IColor
#endregion
#region Operators
public static bool operator ==(ColorRGB left, ColorRGB right) => left.Equals(right);
public static bool operator !=(ColorRGB left, ColorRGB right) => !left.Equals(right);
#endregion
#region Methods
/// <inheritdoc />
public bool Equals(IColor? other) => (other != null) && (R == other.R) && (G == other.G) && (B == other.B) && (A == other.A);
/// <inheritdoc />
public bool Equals(ColorRGB other) => (_r == other._r) && (_g == other._g) && (_b == other._b);
/// <inheritdoc />
public override bool Equals(object? obj) => obj is ColorRGB other && Equals(other);
/// <inheritdoc />
public override int GetHashCode() => HashCode.Combine(_r, _g, _b);
/// <inheritdoc />
public override string ToString() => $"[A: {A}, R: {R}, G: {G}, B: {B}]";

View File

@ -23,7 +23,7 @@ namespace HPPH;
[DebuggerDisplay("[A: {A}, R: {R}, G: {G}, B: {B}]")]
[SkipLocalsInit]
[StructLayout(LayoutKind.Sequential)]
public readonly partial struct ColorRGBA(byte r, byte g, byte b, byte a) : IColor
public readonly partial struct ColorRGBA(byte r, byte g, byte b, byte a) : IColor, IEquatable<ColorRGBA>
{
#region Properties & Fields
@ -49,11 +49,27 @@ public readonly partial struct ColorRGBA(byte r, byte g, byte b, byte a) : IColo
#endregion
#region Operators
public static bool operator ==(ColorRGBA left, ColorRGBA right) => left.Equals(right);
public static bool operator !=(ColorRGBA left, ColorRGBA right) => !left.Equals(right);
#endregion
#region Methods
/// <inheritdoc />
public bool Equals(IColor? other) => (other != null) && (R == other.R) && (G == other.G) && (B == other.B) && (A == other.A);
/// <inheritdoc />
public bool Equals(ColorRGBA other) => (_r == other._r) && (_g == other._g) && (_b == other._b)&& (_a== other._a);
/// <inheritdoc />
public override bool Equals(object? obj) => obj is ColorRGBA other && Equals(other);
/// <inheritdoc />
public override int GetHashCode() => HashCode.Combine(_r, _g, _b, _a);
/// <inheritdoc />
public override string ToString() => $"[A: {A}, R: {R}, G: {G}, B: {B}]";

View File

@ -46,6 +46,10 @@
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
</PropertyGroup>
<ItemGroup>
<GlobalAnalyzerConfigFiles Include="../Analyzers.globalconfig" />
</ItemGroup>
<PropertyGroup Condition="'$(Configuration)'=='Debug'">
<DefineConstants>$(DefineConstants);TRACE;DEBUG</DefineConstants>
<DebugSymbols>true</DebugSymbols>

View File

@ -40,13 +40,15 @@ public sealed class Image<T> : IImage<T>, IEquatable<Image<T>>
/// <inheritdoc />
IColor IImage.this[int x, int y] => this[x, y];
#pragma warning disable CA2208 // Not ideal, but splitting up all the checks introduces quite some overhead :(
/// <inheritdoc />
public ref readonly T this[int x, int y]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if ((x < 0) || (y < 0) || (x >= Width) || (y >= Height)) throw new IndexOutOfRangeException();
if ((x < 0) || (y < 0) || (x >= Width) || (y >= Height)) throw new ArgumentOutOfRangeException();
return ref Unsafe.Add(ref Unsafe.As<byte, T>(ref Unsafe.Add(ref MemoryMarshal.GetReference(_buffer.AsSpan()), (nint)(uint)((_y + y) * _stride))), (nint)(uint)(_x + x));
}
@ -58,7 +60,7 @@ public sealed class Image<T> : IImage<T>, IEquatable<Image<T>>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if ((x < 0) || (y < 0) || (width <= 0) || (height <= 0) || ((x + width) > Width) || ((y + height) > Height)) throw new IndexOutOfRangeException();
if ((x < 0) || (y < 0) || (width <= 0) || (height <= 0) || ((x + width) > Width) || ((y + height) > Height)) throw new ArgumentOutOfRangeException();
return new Image<T>(_buffer, _x + x, _y + y, width, height, _stride);
}
@ -70,12 +72,14 @@ public sealed class Image<T> : IImage<T>, IEquatable<Image<T>>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if ((x < 0) || (y < 0) || (width <= 0) || (height <= 0) || ((x + width) > Width) || ((y + height) > Height)) throw new IndexOutOfRangeException();
if ((x < 0) || (y < 0) || (width <= 0) || (height <= 0) || ((x + width) > Width) || ((y + height) > Height)) throw new ArgumentOutOfRangeException();
return new RefImage<T>(_buffer, _x + x, _y + y, width, height, _stride);
}
}
#pragma warning restore CA2208
/// <inheritdoc />
IImageRows IImage.Rows => new IColorImageRows<T>(_buffer, _x, _y, Width, Height, _stride);
@ -127,6 +131,7 @@ public sealed class Image<T> : IImage<T>, IEquatable<Image<T>>
public static Image<T> Wrap(byte[] buffer, int width, int height, int stride)
{
ArgumentNullException.ThrowIfNull(buffer, nameof(buffer));
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.");

View File

@ -27,7 +27,7 @@ public readonly ref struct ImageColumn<T>
{
get
{
if ((y < 0) || (y >= _length)) throw new IndexOutOfRangeException();
if ((y < 0) || (y >= _length)) throw new ArgumentOutOfRangeException(nameof(y));
return ref Unsafe.As<byte, T>(ref Unsafe.Add(ref MemoryMarshal.GetReference(_buffer), (nint)(uint)(_start + (y * _step))));
}
@ -114,7 +114,7 @@ public readonly ref struct ImageColumn<T>
//HACK DarthAffe 14.07.2024: Not nice, should be removed once ref structs are able to implement interfaces (https://github.com/dotnet/csharplang/blob/main/proposals/ref-struct-interfaces.md)
[SkipLocalsInit]
internal class IColorImageColumn<T> : IImageColumn
internal sealed class IColorImageColumn<T> : IImageColumn
where T : struct, IColor
{
#region Properties & Fields
@ -139,7 +139,7 @@ internal class IColorImageColumn<T> : IImageColumn
{
get
{
if ((y < 0) || (y >= _length)) throw new IndexOutOfRangeException();
if ((y < 0) || (y >= _length)) throw new ArgumentOutOfRangeException(nameof(y));
return Unsafe.As<byte, T>(ref Unsafe.Add(ref MemoryMarshal.GetReference(_buffer.AsSpan()), _start + (y * _step)));
}

View File

@ -28,7 +28,7 @@ public readonly ref struct ImageColumns<T>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if ((column < 0) || (column >= _width)) throw new IndexOutOfRangeException();
if ((column < 0) || (column >= _width)) throw new ArgumentOutOfRangeException(nameof(column));
return new ImageColumn<T>(_data, (_y * _stride) + ((column + _x) * _bpp), _height, _stride);
}
@ -100,7 +100,7 @@ public readonly ref struct ImageColumns<T>
//HACK DarthAffe 14.07.2024: Not nice, should be removed once ref structs are able to implement interfaces (https://github.com/dotnet/csharplang/blob/main/proposals/ref-struct-interfaces.md)
[SkipLocalsInit]
internal class IColorImageColumns<T> : IImageColumns
internal sealed class IColorImageColumns<T> : IImageColumns
where T : struct, IColor
{
#region Properties & Fields
@ -126,7 +126,7 @@ internal class IColorImageColumns<T> : IImageColumns
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if ((column < 0) || (column >= _width)) throw new IndexOutOfRangeException();
if ((column < 0) || (column >= _width)) throw new ArgumentOutOfRangeException(nameof(column));
return new IColorImageColumn<T>(_data, (_y * _stride) + ((column + _x) * _bpp), _height, _stride);
}

View File

@ -26,7 +26,7 @@ public readonly ref struct ImageRow<T>
{
get
{
if ((x < 0) || (x >= _length)) throw new IndexOutOfRangeException();
if ((x < 0) || (x >= _length)) throw new ArgumentOutOfRangeException(nameof(x));
return ref Unsafe.Add(ref Unsafe.As<byte, T>(ref Unsafe.Add(ref MemoryMarshal.GetReference(_buffer), (nint)(uint)_start)), (nint)(uint)x);
}
@ -108,7 +108,7 @@ public readonly ref struct ImageRow<T>
//HACK DarthAffe 14.07.2024: Not nice, should be removed once ref structs are able to implement interfaces (https://github.com/dotnet/csharplang/blob/main/proposals/ref-struct-interfaces.md)
[SkipLocalsInit]
internal class IColorImageRow<T> : IImageRow
internal sealed class IColorImageRow<T> : IImageRow
where T : struct, IColor
{
#region Properties & Fields
@ -132,7 +132,7 @@ internal class IColorImageRow<T> : IImageRow
{
get
{
if ((x < 0) || (x >= _length)) throw new IndexOutOfRangeException();
if ((x < 0) || (x >= _length)) throw new ArgumentOutOfRangeException(nameof(x));
return Unsafe.Add(ref Unsafe.As<byte, T>(ref Unsafe.Add(ref MemoryMarshal.GetReference(_buffer.AsSpan()), (nint)(uint)_start)), (nint)(uint)x);
}

View File

@ -28,7 +28,7 @@ public readonly ref struct ImageRows<T>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if ((row < 0) || (row >= _height)) throw new IndexOutOfRangeException();
if ((row < 0) || (row >= _height)) throw new ArgumentOutOfRangeException(nameof(row));
return new ImageRow<T>(_data, ((row + _y) * _stride) + (_x * _bpp), _width);
}
@ -102,7 +102,7 @@ public readonly ref struct ImageRows<T>
//HACK DarthAffe 14.07.2024: Not nice, should be removed once ref structs are able to implement interfaces (https://github.com/dotnet/csharplang/blob/main/proposals/ref-struct-interfaces.md)
[SkipLocalsInit]
internal class IColorImageRows<T> : IImageRows
internal sealed class IColorImageRows<T> : IImageRows
where T : struct, IColor
{
#region Properties & Fields
@ -128,7 +128,7 @@ internal class IColorImageRows<T> : IImageRows
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if ((row < 0) || (row >= _height)) throw new IndexOutOfRangeException();
if ((row < 0) || (row >= _height)) throw new ArgumentOutOfRangeException(nameof(row));
return new IColorImageRow<T>(_data, ((row + _y) * _stride) + (_x * _bpp), _width);
}

View File

@ -34,12 +34,14 @@ public readonly ref struct RefImage<T>
#region Indexer
#pragma warning disable CA2208 // Not ideal, but splitting up all the checks introduces quite some overhead :(
public ref readonly T this[int x, int y]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if ((x < 0) || (y < 0) || (x >= Width) || (y >= Height)) throw new IndexOutOfRangeException();
if ((x < 0) || (y < 0) || (x >= Width) || (y >= Height)) throw new ArgumentOutOfRangeException();
return ref Unsafe.Add(ref Unsafe.As<byte, T>(ref Unsafe.Add(ref MemoryMarshal.GetReference(_data), (nint)(uint)((_y + y) * RawStride))), (nint)(uint)(_x + x));
}
@ -50,12 +52,14 @@ public readonly ref struct RefImage<T>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if ((x < 0) || (y < 0) || (width <= 0) || (height <= 0) || ((x + width) > Width) || ((y + height) > Height)) throw new IndexOutOfRangeException();
if ((x < 0) || (y < 0) || (width <= 0) || (height <= 0) || ((x + width) > Width) || ((y + height) > Height)) throw new ArgumentOutOfRangeException();
return new RefImage<T>(_data, _x + x, _y + y, width, height, RawStride);
}
}
#pragma warning restore CA2208
public ImageRows<T> Rows
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]

View File

@ -39,7 +39,11 @@ public static partial class PixelHelper
public static T Average<T>(this IImage<T> image)
where T : struct, IColor
=> image.AsRefImage().Average();
{
ArgumentNullException.ThrowIfNull(image, nameof(image));
return image.AsRefImage().Average();
}
public static T Average<T>(this RefImage<T> image)
where T : struct, IColor

View File

@ -40,7 +40,11 @@ public static unsafe partial class PixelHelper
public static IMinMax MinMax<T>(this IImage<T> image)
where T : struct, IColor
=> image.AsRefImage().MinMax();
{
ArgumentNullException.ThrowIfNull(image, nameof(image));
return image.AsRefImage().MinMax();
}
public static IMinMax MinMax<T>(this RefImage<T> image)
where T : struct, IColor

View File

@ -1,7 +1,6 @@
using System.Buffers;
using System.Numerics;
using System.Runtime.InteropServices;
using static System.Net.Mime.MediaTypeNames;
namespace HPPH;
@ -40,7 +39,11 @@ public static partial class PixelHelper
public static T[] CreateColorPalette<T>(this IImage<T> image, int paletteSize)
where T : unmanaged, IColor
=> image.AsRefImage().CreateColorPalette(paletteSize);
{
ArgumentNullException.ThrowIfNull(image, nameof(image));
return image.AsRefImage().CreateColorPalette(paletteSize);
}
public static T[] CreateColorPalette<T>(this RefImage<T> image, int paletteSize)
where T : unmanaged, IColor
@ -165,7 +168,11 @@ public static partial class PixelHelper
public static T[] CreateSimpleColorPalette<T>(this IImage<T> image, int paletteSize)
where T : unmanaged, IColor
=> image.AsRefImage().CreateSimpleColorPalette(paletteSize);
{
ArgumentNullException.ThrowIfNull(image, nameof(image));
return image.AsRefImage().CreateSimpleColorPalette(paletteSize);
}
public static T[] CreateSimpleColorPalette<T>(this RefImage<T> image, int paletteSize)
where T : unmanaged, IColor

View File

@ -41,7 +41,11 @@ public static unsafe partial class PixelHelper
public static ISum Sum<T>(this IImage<T> image)
where T : struct, IColor
=> image.AsRefImage().Sum();
{
ArgumentNullException.ThrowIfNull(image, nameof(image));
return image.AsRefImage().Sum();
}
public static ISum Sum<T>(this RefImage<T> image)
where T : struct, IColor