diff --git a/src/ImageSharp/Common/Helpers/HexConverter.cs b/src/ImageSharp/Common/Helpers/HexConverter.cs index 3c863cc37..590b9d63c 100644 --- a/src/ImageSharp/Common/Helpers/HexConverter.cs +++ b/src/ImageSharp/Common/Helpers/HexConverter.cs @@ -16,12 +16,12 @@ internal static class HexConverter /// The number of bytes written to . public static int HexStringToBytes(ReadOnlySpan chars, Span bytes) { - if ((chars.Length & 1) != 0) // bit-hack for % 2 + if ((chars.Length & 1 /* bit-hack for % 2 */) != 0) { throw new ArgumentException("Input string length must be a multiple of 2", nameof(chars)); } - if ((bytes.Length << 1) < chars.Length) // bit-hack for * 2 + if ((bytes.Length << 1 /* bit-hack for * 2 */) < chars.Length) { throw new ArgumentException("Output span must be at least half the length of the input string"); } diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/ArithmeticScanDecoder.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/ArithmeticScanDecoder.cs index 1b043b68f..423e56378 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/ArithmeticScanDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/ArithmeticScanDecoder.cs @@ -53,9 +53,6 @@ internal class ArithmeticScanDecoder : IJpegScanDecoder private ArithmeticDecodingTable[] acDecodingTables; - // Use C#'s optimization to refer to assembly's data segment, no allocation occurs. - private ReadOnlySpan fixedBin => new byte[] { 113, 0, 0, 0 }; - private readonly CancellationToken cancellationToken; private static readonly int[] ArithmeticTable = @@ -232,7 +229,13 @@ internal class ArithmeticScanDecoder : IJpegScanDecoder } } - private ref byte GetFixedBinReference() => ref MemoryMarshal.GetReference(fixedBin); + private static ref byte GetFixedBinReference() + { + // This uses C#'s optimization to refer to the static data segment of the assembly. + // No allocation occurs. + ReadOnlySpan fixedBin = new byte[] { 113, 0, 0, 0 }; + return ref MemoryMarshal.GetReference(fixedBin); + } /// /// Decodes the entropy coded data. @@ -776,7 +779,7 @@ internal class ArithmeticScanDecoder : IJpegScanDecoder else { // Refinement scan. - ref byte st = ref this.GetFixedBinReference(); + ref byte st = ref GetFixedBinReference(); blockDataRef |= (short)(this.DecodeBinaryDecision(ref reader, ref st) << this.SuccessiveLow); } @@ -822,7 +825,7 @@ internal class ArithmeticScanDecoder : IJpegScanDecoder // Figure F.21: Decoding nonzero value v. // Figure F.22: Decoding the sign of v. - int sign = this.DecodeBinaryDecision(ref reader, ref this.GetFixedBinReference()); + int sign = this.DecodeBinaryDecision(ref reader, ref GetFixedBinReference()); st = ref Unsafe.Add(ref st, 2); // Figure F.23: Decoding the magnitude category of v. @@ -918,7 +921,7 @@ internal class ArithmeticScanDecoder : IJpegScanDecoder if (this.DecodeBinaryDecision(ref reader, ref Unsafe.Add(ref st, 1)) != 0) { - bool flag = this.DecodeBinaryDecision(ref reader, ref this.GetFixedBinReference()) != 0; + bool flag = this.DecodeBinaryDecision(ref reader, ref GetFixedBinReference()) != 0; coef = (short)(coef + (flag ? m1 : p1)); break; @@ -1048,7 +1051,7 @@ internal class ArithmeticScanDecoder : IJpegScanDecoder // Figure F.21: Decoding nonzero value v. // Figure F.22: Decoding the sign of v. - int sign = this.DecodeBinaryDecision(ref reader, ref this.GetFixedBinReference()); + int sign = this.DecodeBinaryDecision(ref reader, ref GetFixedBinReference()); st = ref Unsafe.Add(ref st, 2); // Figure F.23: Decoding the magnitude category of v. diff --git a/src/ImageSharp/Formats/Webp/AlphaDecoder.cs b/src/ImageSharp/Formats/Webp/AlphaDecoder.cs index 637a38d1e..2678a6f70 100644 --- a/src/ImageSharp/Formats/Webp/AlphaDecoder.cs +++ b/src/ImageSharp/Formats/Webp/AlphaDecoder.cs @@ -376,7 +376,7 @@ internal class AlphaDecoder : IDisposable Unsafe.As>(ref outputRef) = c0; } - for (; i < (uint) width; i++) + for (; i < (uint)width; i++) { dst[(int)i] = (byte)(prev[(int)i] + input[(int)i]); } diff --git a/src/ImageSharp/Formats/Webp/Lossy/LossyUtils.cs b/src/ImageSharp/Formats/Webp/Lossy/LossyUtils.cs index 565e4c029..2dc2881b0 100644 --- a/src/ImageSharp/Formats/Webp/Lossy/LossyUtils.cs +++ b/src/ImageSharp/Formats/Webp/Lossy/LossyUtils.cs @@ -1553,7 +1553,7 @@ internal static class LossyUtils Unsafe.As>(ref Unsafe.Add(ref outputRef, (uint)(offset - (3 * stride)))) = p2.AsInt32(); Unsafe.As>(ref Unsafe.Add(ref outputRef, (uint)(offset - (2 * stride)))) = p1.AsInt32(); Unsafe.As>(ref Unsafe.Add(ref outputRef, (uint)(offset - stride))) = p0.AsInt32(); - Unsafe.As>(ref Unsafe.Add(ref outputRef, (uint)(offset))) = q0.AsInt32(); + Unsafe.As>(ref Unsafe.Add(ref outputRef, (uint)offset)) = q0.AsInt32(); Unsafe.As>(ref Unsafe.Add(ref outputRef, (uint)(offset + stride))) = q1.AsInt32(); Unsafe.As>(ref Unsafe.Add(ref outputRef, (uint)(offset + (2 * stride)))) = q2.AsInt32(); } @@ -1599,7 +1599,7 @@ internal static class LossyUtils if (Sse2.IsSupported) { ref byte pRef = ref MemoryMarshal.GetReference(p); - Vector128 p3 = Unsafe.As>(ref Unsafe.Add(ref pRef, (uint)(offset))); + Vector128 p3 = Unsafe.As>(ref Unsafe.Add(ref pRef, (uint)offset)); Vector128 p2 = Unsafe.As>(ref Unsafe.Add(ref pRef, (uint)(offset + stride))); Vector128 p1 = Unsafe.As>(ref Unsafe.Add(ref pRef, (uint)(offset + (2 * stride)))); Vector128 p0 = Unsafe.As>(ref Unsafe.Add(ref pRef, (uint)(offset + (3 * stride))));