From cddb82e07e2acf14ea85b952c138cdf3d5724d86 Mon Sep 17 00:00:00 2001 From: Darth Affe Date: Sun, 11 Aug 2024 17:56:58 +0200 Subject: [PATCH 1/2] Enabled the full set of .NET analyzers --- HPPH.SkiaSharp/HPPH.SkiaSharp.csproj | 3 +++ HPPH.System.Drawing/HPPH.System.Drawing.csproj | 3 +++ HPPH/HPPH.csproj | 3 +++ 3 files changed, 9 insertions(+) diff --git a/HPPH.SkiaSharp/HPPH.SkiaSharp.csproj b/HPPH.SkiaSharp/HPPH.SkiaSharp.csproj index deb82ef..3505eba 100644 --- a/HPPH.SkiaSharp/HPPH.SkiaSharp.csproj +++ b/HPPH.SkiaSharp/HPPH.SkiaSharp.csproj @@ -6,6 +6,9 @@ enable true + true + latest-all + Darth Affe Wyrez en-US diff --git a/HPPH.System.Drawing/HPPH.System.Drawing.csproj b/HPPH.System.Drawing/HPPH.System.Drawing.csproj index 10cea48..5d4b0ee 100644 --- a/HPPH.System.Drawing/HPPH.System.Drawing.csproj +++ b/HPPH.System.Drawing/HPPH.System.Drawing.csproj @@ -6,6 +6,9 @@ enable true + true + latest-all + Darth Affe Wyrez en-US diff --git a/HPPH/HPPH.csproj b/HPPH/HPPH.csproj index 3ae3dc7..ef2c590 100644 --- a/HPPH/HPPH.csproj +++ b/HPPH/HPPH.csproj @@ -7,6 +7,9 @@ true true Generated + + true + latest-all Darth Affe Wyrez From b66fc3c9e97c240baf44264db68af5963d3a0c8f Mon Sep 17 00:00:00 2001 From: Darth Affe Date: Sun, 11 Aug 2024 20:11:46 +0200 Subject: [PATCH 2/2] Fixed a lot of isssues brought up by the analyzers --- Analyzers.globalconfig | 7 ++++ HPPH.Benchmark/BenchmarkHelper.cs | 4 ++- HPPH.Generators/Features/Colors.cs | 36 +++++++++++++++++-- HPPH.SkiaSharp/HPPH.SkiaSharp.csproj | 4 +++ HPPH.SkiaSharp/ImageExtension.cs | 17 +++++++-- .../HPPH.System.Drawing.csproj | 4 +++ HPPH.System.Drawing/ImageExtension.cs | 6 ++++ .../ColorABGR.g.cs | 18 +++++++++- .../ColorARGB.g.cs | 18 +++++++++- .../ColorBGR.g.cs | 18 +++++++++- .../ColorBGRA.g.cs | 18 +++++++++- .../ColorRGB.g.cs | 18 +++++++++- .../ColorRGBA.g.cs | 18 +++++++++- HPPH/HPPH.csproj | 6 +++- HPPH/Images/Image.cs | 11 ++++-- HPPH/Images/ImageColumn.cs | 6 ++-- HPPH/Images/ImageColumns.cs | 6 ++-- HPPH/Images/ImageRow.cs | 6 ++-- HPPH/Images/ImageRows.cs | 6 ++-- HPPH/Images/RefImage.cs | 8 +++-- HPPH/PixelHelper.Average.cs | 6 +++- HPPH/PixelHelper.MinMax.cs | 6 +++- HPPH/PixelHelper.Quantize.cs | 13 +++++-- HPPH/PixelHelper.Sum.cs | 6 +++- 24 files changed, 230 insertions(+), 36 deletions(-) create mode 100644 Analyzers.globalconfig diff --git a/Analyzers.globalconfig b/Analyzers.globalconfig new file mode 100644 index 0000000..9d1e979 --- /dev/null +++ b/Analyzers.globalconfig @@ -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 \ No newline at end of file diff --git a/HPPH.Benchmark/BenchmarkHelper.cs b/HPPH.Benchmark/BenchmarkHelper.cs index 13865aa..4763793 100644 --- a/HPPH.Benchmark/BenchmarkHelper.cs +++ b/HPPH.Benchmark/BenchmarkHelper.cs @@ -1,4 +1,6 @@ -using HPPH.System.Drawing; +#pragma warning disable CA1416 + +using HPPH.System.Drawing; namespace HPPH.Benchmark; diff --git a/HPPH.Generators/Features/Colors.cs b/HPPH.Generators/Features/Colors.cs index 574e4b4..9e44537 100644 --- a/HPPH.Generators/Features/Colors.cs +++ b/HPPH.Generators/Features/Colors.cs @@ -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 /// public bool Equals(IColor? other) => (other != null) && (R == other.R) && (G == other.G) && (B == other.B) && (A == other.A); + /// + public bool Equals({{colorFormat.TypeName}} other) => (_{{colorFormat.FirstEntry}} == other._{{colorFormat.FirstEntry}}) && (_{{colorFormat.SecondEntry}} == other._{{colorFormat.SecondEntry}}) && (_{{colorFormat.ThirdEntry}} == other._{{colorFormat.ThirdEntry}}); + + /// + public override bool Equals(object? obj) => obj is {{colorFormat.TypeName}} other && Equals(other); + + /// + public override int GetHashCode() => HashCode.Combine(_{{colorFormat.FirstEntry}}, _{{colorFormat.SecondEntry}}, _{{colorFormat.ThirdEntry}}); + /// 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 /// public bool Equals(IColor? other) => (other != null) && (R == other.R) && (G == other.G) && (B == other.B) && (A == other.A); + /// + 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}}); + + /// + public override bool Equals(object? obj) => obj is {{colorFormat.TypeName}} other && Equals(other); + + /// + public override int GetHashCode() => HashCode.Combine(_{{colorFormat.FirstEntry}}, _{{colorFormat.SecondEntry}}, _{{colorFormat.ThirdEntry}}, _{{colorFormat.FourthEntry}}); + /// public override string ToString() => $"[A: {A}, R: {R}, G: {G}, B: {B}]"; diff --git a/HPPH.SkiaSharp/HPPH.SkiaSharp.csproj b/HPPH.SkiaSharp/HPPH.SkiaSharp.csproj index 3505eba..ef7f601 100644 --- a/HPPH.SkiaSharp/HPPH.SkiaSharp.csproj +++ b/HPPH.SkiaSharp/HPPH.SkiaSharp.csproj @@ -44,6 +44,10 @@ snupkg + + + + $(DefineConstants);TRACE;DEBUG true diff --git a/HPPH.SkiaSharp/ImageExtension.cs b/HPPH.SkiaSharp/ImageExtension.cs index 9ebd9de..034745f 100644 --- a/HPPH.SkiaSharp/ImageExtension.cs +++ b/HPPH.SkiaSharp/ImageExtension.cs @@ -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().CopyTo(new Span((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.Create(MemoryMarshal.Cast(bitmap.Pixels), bitmap.Width, bitmap.Height); + { + ArgumentNullException.ThrowIfNull(bitmap, nameof(bitmap)); + + return Image.Create(MemoryMarshal.Cast(bitmap.Pixels), bitmap.Width, bitmap.Height); + } } \ No newline at end of file diff --git a/HPPH.System.Drawing/HPPH.System.Drawing.csproj b/HPPH.System.Drawing/HPPH.System.Drawing.csproj index 5d4b0ee..39bc3b3 100644 --- a/HPPH.System.Drawing/HPPH.System.Drawing.csproj +++ b/HPPH.System.Drawing/HPPH.System.Drawing.csproj @@ -44,6 +44,10 @@ snupkg + + + + $(DefineConstants);TRACE;DEBUG true diff --git a/HPPH.System.Drawing/ImageExtension.cs b/HPPH.System.Drawing/ImageExtension.cs index 7cf77b0..40f0e55 100644 --- a/HPPH.System.Drawing/ImageExtension.cs +++ b/HPPH.System.Drawing/ImageExtension.cs @@ -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 buffer = new(data.Scan0.ToPointer(), data.Stride * data.Height); diff --git a/HPPH/Generated/HPPH.Generators/HPPH.Generators.ColorSourceGenerator/ColorABGR.g.cs b/HPPH/Generated/HPPH.Generators/HPPH.Generators.ColorSourceGenerator/ColorABGR.g.cs index 386bb16..862e458 100644 --- a/HPPH/Generated/HPPH.Generators/HPPH.Generators.ColorSourceGenerator/ColorABGR.g.cs +++ b/HPPH/Generated/HPPH.Generators/HPPH.Generators.ColorSourceGenerator/ColorABGR.g.cs @@ -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 { #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 /// public bool Equals(IColor? other) => (other != null) && (R == other.R) && (G == other.G) && (B == other.B) && (A == other.A); + /// + public bool Equals(ColorABGR other) => (_a == other._a) && (_b == other._b) && (_g == other._g)&& (_r== other._r); + + /// + public override bool Equals(object? obj) => obj is ColorABGR other && Equals(other); + + /// + public override int GetHashCode() => HashCode.Combine(_a, _b, _g, _r); + /// public override string ToString() => $"[A: {A}, R: {R}, G: {G}, B: {B}]"; diff --git a/HPPH/Generated/HPPH.Generators/HPPH.Generators.ColorSourceGenerator/ColorARGB.g.cs b/HPPH/Generated/HPPH.Generators/HPPH.Generators.ColorSourceGenerator/ColorARGB.g.cs index 14107ce..b068c2e 100644 --- a/HPPH/Generated/HPPH.Generators/HPPH.Generators.ColorSourceGenerator/ColorARGB.g.cs +++ b/HPPH/Generated/HPPH.Generators/HPPH.Generators.ColorSourceGenerator/ColorARGB.g.cs @@ -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 { #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 /// public bool Equals(IColor? other) => (other != null) && (R == other.R) && (G == other.G) && (B == other.B) && (A == other.A); + /// + public bool Equals(ColorARGB other) => (_a == other._a) && (_r == other._r) && (_g == other._g)&& (_b== other._b); + + /// + public override bool Equals(object? obj) => obj is ColorARGB other && Equals(other); + + /// + public override int GetHashCode() => HashCode.Combine(_a, _r, _g, _b); + /// public override string ToString() => $"[A: {A}, R: {R}, G: {G}, B: {B}]"; diff --git a/HPPH/Generated/HPPH.Generators/HPPH.Generators.ColorSourceGenerator/ColorBGR.g.cs b/HPPH/Generated/HPPH.Generators/HPPH.Generators.ColorSourceGenerator/ColorBGR.g.cs index 625b4c1..52e9e73 100644 --- a/HPPH/Generated/HPPH.Generators/HPPH.Generators.ColorSourceGenerator/ColorBGR.g.cs +++ b/HPPH/Generated/HPPH.Generators/HPPH.Generators.ColorSourceGenerator/ColorBGR.g.cs @@ -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 { #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 /// public bool Equals(IColor? other) => (other != null) && (R == other.R) && (G == other.G) && (B == other.B) && (A == other.A); + /// + public bool Equals(ColorBGR other) => (_b == other._b) && (_g == other._g) && (_r == other._r); + + /// + public override bool Equals(object? obj) => obj is ColorBGR other && Equals(other); + + /// + public override int GetHashCode() => HashCode.Combine(_b, _g, _r); + /// public override string ToString() => $"[A: {A}, R: {R}, G: {G}, B: {B}]"; diff --git a/HPPH/Generated/HPPH.Generators/HPPH.Generators.ColorSourceGenerator/ColorBGRA.g.cs b/HPPH/Generated/HPPH.Generators/HPPH.Generators.ColorSourceGenerator/ColorBGRA.g.cs index 4042792..d8ea6db 100644 --- a/HPPH/Generated/HPPH.Generators/HPPH.Generators.ColorSourceGenerator/ColorBGRA.g.cs +++ b/HPPH/Generated/HPPH.Generators/HPPH.Generators.ColorSourceGenerator/ColorBGRA.g.cs @@ -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 { #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 /// public bool Equals(IColor? other) => (other != null) && (R == other.R) && (G == other.G) && (B == other.B) && (A == other.A); + /// + public bool Equals(ColorBGRA other) => (_b == other._b) && (_g == other._g) && (_r == other._r)&& (_a== other._a); + + /// + public override bool Equals(object? obj) => obj is ColorBGRA other && Equals(other); + + /// + public override int GetHashCode() => HashCode.Combine(_b, _g, _r, _a); + /// public override string ToString() => $"[A: {A}, R: {R}, G: {G}, B: {B}]"; diff --git a/HPPH/Generated/HPPH.Generators/HPPH.Generators.ColorSourceGenerator/ColorRGB.g.cs b/HPPH/Generated/HPPH.Generators/HPPH.Generators.ColorSourceGenerator/ColorRGB.g.cs index 63b3596..caf6c4f 100644 --- a/HPPH/Generated/HPPH.Generators/HPPH.Generators.ColorSourceGenerator/ColorRGB.g.cs +++ b/HPPH/Generated/HPPH.Generators/HPPH.Generators.ColorSourceGenerator/ColorRGB.g.cs @@ -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 { #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 /// public bool Equals(IColor? other) => (other != null) && (R == other.R) && (G == other.G) && (B == other.B) && (A == other.A); + /// + public bool Equals(ColorRGB other) => (_r == other._r) && (_g == other._g) && (_b == other._b); + + /// + public override bool Equals(object? obj) => obj is ColorRGB other && Equals(other); + + /// + public override int GetHashCode() => HashCode.Combine(_r, _g, _b); + /// public override string ToString() => $"[A: {A}, R: {R}, G: {G}, B: {B}]"; diff --git a/HPPH/Generated/HPPH.Generators/HPPH.Generators.ColorSourceGenerator/ColorRGBA.g.cs b/HPPH/Generated/HPPH.Generators/HPPH.Generators.ColorSourceGenerator/ColorRGBA.g.cs index bd47a06..7b2340d 100644 --- a/HPPH/Generated/HPPH.Generators/HPPH.Generators.ColorSourceGenerator/ColorRGBA.g.cs +++ b/HPPH/Generated/HPPH.Generators/HPPH.Generators.ColorSourceGenerator/ColorRGBA.g.cs @@ -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 { #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 /// public bool Equals(IColor? other) => (other != null) && (R == other.R) && (G == other.G) && (B == other.B) && (A == other.A); + /// + public bool Equals(ColorRGBA other) => (_r == other._r) && (_g == other._g) && (_b == other._b)&& (_a== other._a); + + /// + public override bool Equals(object? obj) => obj is ColorRGBA other && Equals(other); + + /// + public override int GetHashCode() => HashCode.Combine(_r, _g, _b, _a); + /// public override string ToString() => $"[A: {A}, R: {R}, G: {G}, B: {B}]"; diff --git a/HPPH/HPPH.csproj b/HPPH/HPPH.csproj index ef2c590..a944f36 100644 --- a/HPPH/HPPH.csproj +++ b/HPPH/HPPH.csproj @@ -7,7 +7,7 @@ true true Generated - + true latest-all @@ -46,6 +46,10 @@ snupkg + + + + $(DefineConstants);TRACE;DEBUG true diff --git a/HPPH/Images/Image.cs b/HPPH/Images/Image.cs index f221c15..1bbea09 100644 --- a/HPPH/Images/Image.cs +++ b/HPPH/Images/Image.cs @@ -40,13 +40,15 @@ public sealed class Image : IImage, IEquatable> /// 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 :( + /// 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(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 : IImage, IEquatable> [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(_buffer, _x + x, _y + y, width, height, _stride); } @@ -70,12 +72,14 @@ public sealed class Image : IImage, IEquatable> [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(_buffer, _x + x, _y + y, width, height, _stride); } } +#pragma warning restore CA2208 + /// IImageRows IImage.Rows => new IColorImageRows(_buffer, _x, _y, Width, Height, _stride); @@ -127,6 +131,7 @@ public sealed class Image : IImage, IEquatable> public static Image 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."); diff --git a/HPPH/Images/ImageColumn.cs b/HPPH/Images/ImageColumn.cs index aaf76d2..0e551b5 100644 --- a/HPPH/Images/ImageColumn.cs +++ b/HPPH/Images/ImageColumn.cs @@ -27,7 +27,7 @@ public readonly ref struct ImageColumn { get { - if ((y < 0) || (y >= _length)) throw new IndexOutOfRangeException(); + if ((y < 0) || (y >= _length)) throw new ArgumentOutOfRangeException(nameof(y)); return ref Unsafe.As(ref Unsafe.Add(ref MemoryMarshal.GetReference(_buffer), (nint)(uint)(_start + (y * _step)))); } @@ -114,7 +114,7 @@ public readonly ref struct ImageColumn //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 : IImageColumn +internal sealed class IColorImageColumn : IImageColumn where T : struct, IColor { #region Properties & Fields @@ -139,7 +139,7 @@ internal class IColorImageColumn : IImageColumn { get { - if ((y < 0) || (y >= _length)) throw new IndexOutOfRangeException(); + if ((y < 0) || (y >= _length)) throw new ArgumentOutOfRangeException(nameof(y)); return Unsafe.As(ref Unsafe.Add(ref MemoryMarshal.GetReference(_buffer.AsSpan()), _start + (y * _step))); } diff --git a/HPPH/Images/ImageColumns.cs b/HPPH/Images/ImageColumns.cs index 2514707..936ac3a 100644 --- a/HPPH/Images/ImageColumns.cs +++ b/HPPH/Images/ImageColumns.cs @@ -28,7 +28,7 @@ public readonly ref struct ImageColumns [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(_data, (_y * _stride) + ((column + _x) * _bpp), _height, _stride); } @@ -100,7 +100,7 @@ public readonly ref struct ImageColumns //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 : IImageColumns +internal sealed class IColorImageColumns : IImageColumns where T : struct, IColor { #region Properties & Fields @@ -126,7 +126,7 @@ internal class IColorImageColumns : 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(_data, (_y * _stride) + ((column + _x) * _bpp), _height, _stride); } diff --git a/HPPH/Images/ImageRow.cs b/HPPH/Images/ImageRow.cs index ded5f2b..40235bb 100644 --- a/HPPH/Images/ImageRow.cs +++ b/HPPH/Images/ImageRow.cs @@ -26,7 +26,7 @@ public readonly ref struct ImageRow { 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(ref Unsafe.Add(ref MemoryMarshal.GetReference(_buffer), (nint)(uint)_start)), (nint)(uint)x); } @@ -108,7 +108,7 @@ public readonly ref struct ImageRow //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 : IImageRow +internal sealed class IColorImageRow : IImageRow where T : struct, IColor { #region Properties & Fields @@ -132,7 +132,7 @@ internal class IColorImageRow : 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(ref Unsafe.Add(ref MemoryMarshal.GetReference(_buffer.AsSpan()), (nint)(uint)_start)), (nint)(uint)x); } diff --git a/HPPH/Images/ImageRows.cs b/HPPH/Images/ImageRows.cs index d50569d..6362658 100644 --- a/HPPH/Images/ImageRows.cs +++ b/HPPH/Images/ImageRows.cs @@ -28,7 +28,7 @@ public readonly ref struct ImageRows [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(_data, ((row + _y) * _stride) + (_x * _bpp), _width); } @@ -102,7 +102,7 @@ public readonly ref struct ImageRows //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 : IImageRows +internal sealed class IColorImageRows : IImageRows where T : struct, IColor { #region Properties & Fields @@ -128,7 +128,7 @@ internal class IColorImageRows : 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(_data, ((row + _y) * _stride) + (_x * _bpp), _width); } diff --git a/HPPH/Images/RefImage.cs b/HPPH/Images/RefImage.cs index 8772a10..2b193e6 100644 --- a/HPPH/Images/RefImage.cs +++ b/HPPH/Images/RefImage.cs @@ -34,12 +34,14 @@ public readonly ref struct RefImage #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(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 [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(_data, _x + x, _y + y, width, height, RawStride); } } +#pragma warning restore CA2208 + public ImageRows Rows { [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/HPPH/PixelHelper.Average.cs b/HPPH/PixelHelper.Average.cs index df3ff5d..5ac2c62 100644 --- a/HPPH/PixelHelper.Average.cs +++ b/HPPH/PixelHelper.Average.cs @@ -39,7 +39,11 @@ public static partial class PixelHelper public static T Average(this IImage image) where T : struct, IColor - => image.AsRefImage().Average(); + { + ArgumentNullException.ThrowIfNull(image, nameof(image)); + + return image.AsRefImage().Average(); + } public static T Average(this RefImage image) where T : struct, IColor diff --git a/HPPH/PixelHelper.MinMax.cs b/HPPH/PixelHelper.MinMax.cs index 008817f..86cd3ec 100644 --- a/HPPH/PixelHelper.MinMax.cs +++ b/HPPH/PixelHelper.MinMax.cs @@ -40,7 +40,11 @@ public static unsafe partial class PixelHelper public static IMinMax MinMax(this IImage image) where T : struct, IColor - => image.AsRefImage().MinMax(); + { + ArgumentNullException.ThrowIfNull(image, nameof(image)); + + return image.AsRefImage().MinMax(); + } public static IMinMax MinMax(this RefImage image) where T : struct, IColor diff --git a/HPPH/PixelHelper.Quantize.cs b/HPPH/PixelHelper.Quantize.cs index 21f7354..2908de5 100644 --- a/HPPH/PixelHelper.Quantize.cs +++ b/HPPH/PixelHelper.Quantize.cs @@ -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(this IImage 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(this RefImage image, int paletteSize) where T : unmanaged, IColor @@ -165,7 +168,11 @@ public static partial class PixelHelper public static T[] CreateSimpleColorPalette(this IImage 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(this RefImage image, int paletteSize) where T : unmanaged, IColor diff --git a/HPPH/PixelHelper.Sum.cs b/HPPH/PixelHelper.Sum.cs index 01b6df0..ead249b 100644 --- a/HPPH/PixelHelper.Sum.cs +++ b/HPPH/PixelHelper.Sum.cs @@ -41,7 +41,11 @@ public static unsafe partial class PixelHelper public static ISum Sum(this IImage image) where T : struct, IColor - => image.AsRefImage().Sum(); + { + ArgumentNullException.ThrowIfNull(image, nameof(image)); + + return image.AsRefImage().Sum(); + } public static ISum Sum(this RefImage image) where T : struct, IColor