From b8a76143cc5f0ea070f0b1072c85dd13d4555aa3 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Sun, 5 Feb 2023 22:58:13 +1000 Subject: [PATCH 1/9] Recreate @brianpopow 's changes --- src/ImageSharp/Compression/Zlib/Adler32.cs | 108 ++++++++++++++++-- src/ImageSharp/Compression/Zlib/Crc32.cs | 95 +++++++++++++++ .../Formats/Png/Filters/AverageFilter.cs | 34 +++++- .../Formats/Png/Filters/PaethFilter.cs | 79 +++++++++++++ .../Formats/Png/Filters/SubFilter.cs | 29 +++++ .../Formats/Png/Filters/UpFilter.cs | 46 +++++++- .../Formats/Png/Crc32Tests.cs | 36 ++++-- .../FeatureTesting/FeatureTestRunner.cs | 31 +++-- .../Tests/FeatureTestRunnerTests.cs | 56 ++++++++- 9 files changed, 476 insertions(+), 38 deletions(-) diff --git a/src/ImageSharp/Compression/Zlib/Adler32.cs b/src/ImageSharp/Compression/Zlib/Adler32.cs index 3885ef5755..1b1a77715d 100644 --- a/src/ImageSharp/Compression/Zlib/Adler32.cs +++ b/src/ImageSharp/Compression/Zlib/Adler32.cs @@ -4,6 +4,7 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Runtime.Intrinsics; +using System.Runtime.Intrinsics.Arm; using System.Runtime.Intrinsics.X86; #pragma warning disable IDE0007 // Use implicit type @@ -95,7 +96,7 @@ internal static class Adler32 Vector128 tap1 = Sse2.LoadVector128((sbyte*)tapPtr); Vector128 tap2 = Sse2.LoadVector128((sbyte*)(tapPtr + 0x10)); Vector128 zero = Vector128.Zero; - var ones = Vector128.Create((short)1); + Vector128 ones = Vector128.Create((short)1); while (blocks > 0) { @@ -179,13 +180,13 @@ internal static class Adler32 byte* localBufferPtr = bufferPtr; Vector256 zero = Vector256.Zero; - var dot3v = Vector256.Create((short)1); - var dot2v = Vector256.Create(32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1); + Vector256 dot3v = Vector256.Create((short)1); + Vector256 dot2v = Vector256.Create(32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1); // Process n blocks of data. At most NMAX data bytes can be // processed before s2 must be reduced modulo BASE. - var vs1 = Vector256.CreateScalar(s1); - var vs2 = Vector256.CreateScalar(s2); + Vector256 vs1 = Vector256.CreateScalar(s1); + Vector256 vs2 = Vector256.CreateScalar(s2); while (length >= 32) { @@ -243,6 +244,100 @@ internal static class Adler32 } } + // Based on: https://github.com/chromium/chromium/blob/master/third_party/zlib/adler32_simd.c + [MethodImpl(InliningOptions.HotPath | InliningOptions.ShortMethod)] + private static unsafe uint CalculateArm(uint adler, ReadOnlySpan buffer) + { + // Split Adler-32 into component sums. + uint s1 = adler & 0xFFFF; + uint s2 = (adler >> 16) & 0xFFFF; + uint length = (uint)buffer.Length; + + // Process the data in blocks. + long blocks = length / BlockSize; + length -= (uint)(blocks * BlockSize); + fixed (byte* bufferPtr = &MemoryMarshal.GetReference(buffer)) + { + byte* localBufferPtr = bufferPtr; + + while (blocks != 0) + { + uint n = NMAX / BlockSize; + if (n > blocks) + { + n = (uint)blocks; + } + + blocks -= n; + + // Process n blocks of data. At most nMax data bytes can be + // processed before s2 must be reduced modulo Base. + Vector128 vs1 = Vector128.Zero; + Vector128 vs2 = vs1.WithElement(3, s1 * n); + Vector128 vColumnSum1 = Vector128.Zero; + Vector128 vColumnSum2 = Vector128.Zero; + Vector128 vColumnSum3 = Vector128.Zero; + Vector128 vColumnSum4 = Vector128.Zero; + + do + { + // Load 32 input bytes. + Vector128 bytes1 = AdvSimd.LoadVector128(localBufferPtr).AsUInt16(); + Vector128 bytes2 = AdvSimd.LoadVector128(localBufferPtr + 0x10).AsUInt16(); + + // Add previous block byte sum to v_s2. + vs2 = AdvSimd.Add(vs2, vs1); + + // Horizontally add the bytes for s1. + vs1 = AdvSimd.AddPairwiseWideningAndAdd( + vs1.AsUInt32(), + AdvSimd.AddPairwiseWideningAndAdd(AdvSimd.AddPairwiseWidening(bytes1.AsByte()).AsUInt16(), bytes2.AsByte())); + + // Vertically add the bytes for s2. + vColumnSum1 = AdvSimd.AddWideningLower(vColumnSum1, bytes1.GetLower().AsByte()); + vColumnSum2 = AdvSimd.AddWideningLower(vColumnSum2, bytes1.GetUpper().AsByte()); + vColumnSum3 = AdvSimd.AddWideningLower(vColumnSum3, bytes2.GetLower().AsByte()); + vColumnSum4 = AdvSimd.AddWideningLower(vColumnSum4, bytes2.GetUpper().AsByte()); + + localBufferPtr += BlockSize; + } + while (--n > 0); + + vs2 = AdvSimd.ShiftLeftLogical(vs2, 5); + + // Multiply-add bytes by [ 32, 31, 30, ... ] for s2. + vs2 = AdvSimd.MultiplyWideningLowerAndAdd(vs2, vColumnSum1.GetLower(), Vector64.Create((ushort)32, 31, 30, 29)); + vs2 = AdvSimd.MultiplyWideningLowerAndAdd(vs2, vColumnSum1.GetUpper(), Vector64.Create((ushort)28, 27, 26, 25)); + vs2 = AdvSimd.MultiplyWideningLowerAndAdd(vs2, vColumnSum2.GetLower(), Vector64.Create((ushort)24, 23, 22, 21)); + vs2 = AdvSimd.MultiplyWideningLowerAndAdd(vs2, vColumnSum2.GetUpper(), Vector64.Create((ushort)20, 19, 18, 17)); + vs2 = AdvSimd.MultiplyWideningLowerAndAdd(vs2, vColumnSum3.GetLower(), Vector64.Create((ushort)16, 15, 14, 13)); + vs2 = AdvSimd.MultiplyWideningLowerAndAdd(vs2, vColumnSum3.GetUpper(), Vector64.Create((ushort)12, 11, 10, 9)); + vs2 = AdvSimd.MultiplyWideningLowerAndAdd(vs2, vColumnSum4.GetLower(), Vector64.Create((ushort)8, 7, 6, 5)); + vs2 = AdvSimd.MultiplyWideningLowerAndAdd(vs2, vColumnSum4.GetUpper(), Vector64.Create((ushort)4, 3, 2, 1)); + + // Sum epi32 ints v_s1(s2) and accumulate in s1(s2). + Vector64 sum1 = AdvSimd.AddPairwise(vs1.GetLower(), vs1.GetUpper()); + Vector64 sum2 = AdvSimd.AddPairwise(vs2.GetLower(), vs2.GetUpper()); + Vector64 s1s2 = AdvSimd.AddPairwise(sum1, sum2); + + // Store the results. + s1 += AdvSimd.Extract(s1s2, 0); + s2 += AdvSimd.Extract(s1s2, 1); + + // Reduce. + s1 %= BASE; + s2 %= BASE; + } + + if (length > 0) + { + HandleLeftOver(localBufferPtr, length, ref s1, ref s2); + } + + return s1 | (s2 << 16); + } + } + private static unsafe void HandleLeftOver(byte* localBufferPtr, uint length, ref uint s1, ref uint s2) { if (length >= 16) @@ -286,7 +381,6 @@ internal static class Adler32 { uint s1 = adler & 0xFFFF; uint s2 = (adler >> 16) & 0xFFFF; - uint k; fixed (byte* bufferPtr = buffer) { @@ -295,7 +389,7 @@ internal static class Adler32 while (length > 0) { - k = length < NMAX ? length : NMAX; + uint k = length < NMAX ? length : NMAX; length -= k; while (k >= 16) diff --git a/src/ImageSharp/Compression/Zlib/Crc32.cs b/src/ImageSharp/Compression/Zlib/Crc32.cs index b8665bd43a..c85d58df5b 100644 --- a/src/ImageSharp/Compression/Zlib/Crc32.cs +++ b/src/ImageSharp/Compression/Zlib/Crc32.cs @@ -5,6 +5,7 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Runtime.Intrinsics; using System.Runtime.Intrinsics.X86; +using ArmCrc32 = System.Runtime.Intrinsics.Arm.Crc32; namespace SixLabors.ImageSharp.Compression.Zlib; @@ -187,6 +188,100 @@ internal static partial class Crc32 } } + [MethodImpl(InliningOptions.HotPath | InliningOptions.ShortMethod)] + private static unsafe uint CalculateArm(uint crc, ReadOnlySpan buffer) + { + fixed (byte* bufferPtr = buffer) + { + byte* localBufferPtr = bufferPtr; + int len = buffer.Length; + + while (len > 0 && ((ulong)localBufferPtr & 3) != 0) + { + crc = ArmCrc32.ComputeCrc32(crc, *localBufferPtr++); + len--; + } + + uint* intBufferPtr = (uint*)localBufferPtr; + + while (len >= 8 * sizeof(uint)) + { + crc = ArmCrc32.ComputeCrc32(crc, *intBufferPtr++); + crc = ArmCrc32.ComputeCrc32(crc, *intBufferPtr++); + crc = ArmCrc32.ComputeCrc32(crc, *intBufferPtr++); + crc = ArmCrc32.ComputeCrc32(crc, *intBufferPtr++); + crc = ArmCrc32.ComputeCrc32(crc, *intBufferPtr++); + crc = ArmCrc32.ComputeCrc32(crc, *intBufferPtr++); + crc = ArmCrc32.ComputeCrc32(crc, *intBufferPtr++); + crc = ArmCrc32.ComputeCrc32(crc, *intBufferPtr++); + len -= 8 * sizeof(uint); + } + + while (len >= sizeof(uint)) + { + crc = ArmCrc32.ComputeCrc32(crc, *intBufferPtr++); + len -= sizeof(uint); + } + + localBufferPtr = (byte*)intBufferPtr; + + while (len > 0) + { + crc = ArmCrc32.ComputeCrc32(crc, *localBufferPtr++); + len--; + } + + return crc; + } + } + + [MethodImpl(InliningOptions.HotPath | InliningOptions.ShortMethod)] + private static unsafe uint CalculateArm64(uint crc, ReadOnlySpan buffer) + { + fixed (byte* bufferPtr = buffer) + { + byte* localBufferPtr = bufferPtr; + int len = buffer.Length; + + while (len > 0 && ((ulong)localBufferPtr & 7) != 0) + { + crc = ArmCrc32.ComputeCrc32(crc, *localBufferPtr++); + len--; + } + + ulong* longBufferPtr = (ulong*)localBufferPtr; + + while (len >= 8 * sizeof(ulong)) + { + crc = ArmCrc32.Arm64.ComputeCrc32(crc, *longBufferPtr++); + crc = ArmCrc32.Arm64.ComputeCrc32(crc, *longBufferPtr++); + crc = ArmCrc32.Arm64.ComputeCrc32(crc, *longBufferPtr++); + crc = ArmCrc32.Arm64.ComputeCrc32(crc, *longBufferPtr++); + crc = ArmCrc32.Arm64.ComputeCrc32(crc, *longBufferPtr++); + crc = ArmCrc32.Arm64.ComputeCrc32(crc, *longBufferPtr++); + crc = ArmCrc32.Arm64.ComputeCrc32(crc, *longBufferPtr++); + crc = ArmCrc32.Arm64.ComputeCrc32(crc, *longBufferPtr++); + len -= 8 * sizeof(ulong); + } + + while (len >= sizeof(ulong)) + { + crc = ArmCrc32.Arm64.ComputeCrc32(crc, *longBufferPtr++); + len -= sizeof(ulong); + } + + localBufferPtr = (byte*)longBufferPtr; + + while (len > 0) + { + crc = ArmCrc32.ComputeCrc32(crc, *localBufferPtr++); + len--; + } + + return crc; + } + } + [MethodImpl(InliningOptions.HotPath | InliningOptions.ShortMethod)] private static uint CalculateScalar(uint crc, ReadOnlySpan buffer) { diff --git a/src/ImageSharp/Formats/Png/Filters/AverageFilter.cs b/src/ImageSharp/Formats/Png/Filters/AverageFilter.cs index 7a6ea13ca7..34e05d7796 100644 --- a/src/ImageSharp/Formats/Png/Filters/AverageFilter.cs +++ b/src/ImageSharp/Formats/Png/Filters/AverageFilter.cs @@ -4,6 +4,7 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Runtime.Intrinsics; +using System.Runtime.Intrinsics.Arm; using System.Runtime.Intrinsics.X86; namespace SixLabors.ImageSharp.Formats.Png.Filters; @@ -35,6 +36,10 @@ internal static class AverageFilter { DecodeSse2(scanline, previousScanline); } + else if (AdvSimd.IsSupported && bytesPerPixel is 4) + { + DecodeArm(scanline, previousScanline); + } else { DecodeScalar(scanline, previousScanline, bytesPerPixel); @@ -48,7 +53,7 @@ internal static class AverageFilter ref byte prevBaseRef = ref MemoryMarshal.GetReference(previousScanline); Vector128 d = Vector128.Zero; - var ones = Vector128.Create((byte)1); + Vector128 ones = Vector128.Create((byte)1); int rb = scanline.Length; nint offset = 1; @@ -75,6 +80,33 @@ internal static class AverageFilter } } + public static void DecodeArm(Span scanline, Span previousScanline) + { + ref byte scanBaseRef = ref MemoryMarshal.GetReference(scanline); + ref byte prevBaseRef = ref MemoryMarshal.GetReference(previousScanline); + + Vector64 d = Vector64.Zero; + + int rb = scanline.Length; + int offset = 1; + const int bytesPerBatch = 4; + while (rb >= bytesPerBatch) + { + ref byte scanRef = ref Unsafe.Add(ref scanBaseRef, offset); + Vector64 a = d; + Vector64 b = Vector64.CreateScalar(Unsafe.As(ref Unsafe.Add(ref prevBaseRef, offset))).AsByte(); + d = Vector64.CreateScalar(Unsafe.As(ref scanRef)).AsByte(); + + Vector64 avg = AdvSimd.FusedAddHalving(a, b); + d = AdvSimd.Add(d, avg); + + Unsafe.As(ref scanRef) = d.AsInt32().ToScalar(); + + rb -= bytesPerBatch; + offset += bytesPerBatch; + } + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] private static void DecodeScalar(Span scanline, Span previousScanline, int bytesPerPixel) { diff --git a/src/ImageSharp/Formats/Png/Filters/PaethFilter.cs b/src/ImageSharp/Formats/Png/Filters/PaethFilter.cs index aee68487dd..8998d6bc0f 100644 --- a/src/ImageSharp/Formats/Png/Filters/PaethFilter.cs +++ b/src/ImageSharp/Formats/Png/Filters/PaethFilter.cs @@ -5,6 +5,7 @@ using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Runtime.Intrinsics; +using System.Runtime.Intrinsics.Arm; using System.Runtime.Intrinsics.X86; namespace SixLabors.ImageSharp.Formats.Png.Filters; @@ -38,6 +39,10 @@ internal static class PaethFilter { DecodeSse41(scanline, previousScanline); } + else if (AdvSimd.Arm64.IsSupported && bytesPerPixel is 4) + { + DecodeArm(scanline, previousScanline); + } else { DecodeScalar(scanline, previousScanline, bytesPerPixel); @@ -99,6 +104,80 @@ internal static class PaethFilter } } + public static void DecodeArm(Span scanline, Span previousScanline) + { + ref byte scanBaseRef = ref MemoryMarshal.GetReference(scanline); + ref byte prevBaseRef = ref MemoryMarshal.GetReference(previousScanline); + + Vector128 b = Vector128.Zero; + Vector128 d = Vector128.Zero; + + int rb = scanline.Length; + nint offset = 1; + const int bytesPerBatch = 4; + while (rb >= bytesPerBatch) + { + ref byte scanRef = ref Unsafe.Add(ref scanBaseRef, offset); + Vector128 c = b; + Vector128 a = d; + b = AdvSimd.Arm64.ZipLow( + Vector128.CreateScalar(Unsafe.As(ref Unsafe.Add(ref prevBaseRef, offset))).AsByte(), + Vector128.Zero).AsByte(); + d = AdvSimd.Arm64.ZipLow( + Vector128.CreateScalar(Unsafe.As(ref scanRef)).AsByte(), + Vector128.Zero).AsByte(); + + // (p-a) == (a+b-c - a) == (b-c) + Vector128 pa = AdvSimd.Subtract(b.AsInt16(), c.AsInt16()); + + // (p-b) == (a+b-c - b) == (a-c) + Vector128 pb = AdvSimd.Subtract(a.AsInt16(), c.AsInt16()); + + // (p-c) == (a+b-c - c) == (a+b-c-c) == (b-c)+(a-c) + Vector128 pc = AdvSimd.Add(pa.AsInt16(), pb.AsInt16()); + + pa = AdvSimd.Abs(pa.AsInt16()).AsInt16(); /* |p-a| */ + pb = AdvSimd.Abs(pb.AsInt16()).AsInt16(); /* |p-b| */ + pc = AdvSimd.Abs(pc.AsInt16()).AsInt16(); /* |p-c| */ + + Vector128 smallest = AdvSimd.Min(pc, AdvSimd.Min(pa, pb)); + + // Paeth breaks ties favoring a over b over c. + Vector128 mask = BlendVariable(c, b, AdvSimd.CompareEqual(smallest, pb).AsByte()); + Vector128 nearest = BlendVariable(mask, a, AdvSimd.CompareEqual(smallest, pa).AsByte()); + + d = AdvSimd.Add(d, nearest); + + Vector64 e = AdvSimd.ExtractNarrowingSaturateUnsignedLower(d.AsInt16()); + + Unsafe.As(ref scanRef) = Vector128.Create(e, e).AsInt32().ToScalar(); + + rb -= bytesPerBatch; + offset += bytesPerBatch; + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static Vector128 BlendVariable(Vector128 a, Vector128 b, Vector128 c) + { + // Equivalent of Sse41.BlendVariable: + // Blend packed 8-bit integers from a and b using mask, and store the results in + // dst. + // + // FOR j := 0 to 15 + // i := j*8 + // IF mask[i+7] + // dst[i+7:i] := b[i+7:i] + // ELSE + // dst[i+7:i] := a[i+7:i] + // FI + // ENDFOR + // + // Use a signed shift right to create a mask with the sign bit. + Vector128 mask = AdvSimd.ShiftRightArithmetic(c.AsInt16(), 7); + return AdvSimd.BitwiseSelect(mask, b.AsInt16(), a.AsInt16()).AsByte(); + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] private static void DecodeScalar(Span scanline, Span previousScanline, int bytesPerPixel) { diff --git a/src/ImageSharp/Formats/Png/Filters/SubFilter.cs b/src/ImageSharp/Formats/Png/Filters/SubFilter.cs index dcf55237d3..0f4aa3fcff 100644 --- a/src/ImageSharp/Formats/Png/Filters/SubFilter.cs +++ b/src/ImageSharp/Formats/Png/Filters/SubFilter.cs @@ -5,6 +5,7 @@ using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Runtime.Intrinsics; +using System.Runtime.Intrinsics.Arm; using System.Runtime.Intrinsics.X86; namespace SixLabors.ImageSharp.Formats.Png.Filters; @@ -29,6 +30,10 @@ internal static class SubFilter { DecodeSse2(scanline); } + else if (AdvSimd.IsSupported && bytesPerPixel is 4) + { + DecodeArm(scanline); + } else { DecodeScalar(scanline, bytesPerPixel); @@ -58,6 +63,30 @@ internal static class SubFilter } } + public static void DecodeArm(Span scanline) + { + ref byte scanBaseRef = ref MemoryMarshal.GetReference(scanline); + + Vector64 d = Vector64.Zero; + + int rb = scanline.Length; + int offset = 1; + const int bytesPerBatch = 4; + while (rb >= bytesPerBatch) + { + ref byte scanRef = ref Unsafe.Add(ref scanBaseRef, offset); + Vector64 a = d; + d = Vector64.CreateScalar(Unsafe.As(ref scanRef)).AsByte(); + + d = AdvSimd.Add(d, a); + + Unsafe.As(ref scanRef) = d.AsInt32().ToScalar(); + + rb -= bytesPerBatch; + offset += bytesPerBatch; + } + } + private static void DecodeScalar(Span scanline, int bytesPerPixel) { ref byte scanBaseRef = ref MemoryMarshal.GetReference(scanline); diff --git a/src/ImageSharp/Formats/Png/Filters/UpFilter.cs b/src/ImageSharp/Formats/Png/Filters/UpFilter.cs index 833563655c..f90b129b62 100644 --- a/src/ImageSharp/Formats/Png/Filters/UpFilter.cs +++ b/src/ImageSharp/Formats/Png/Filters/UpFilter.cs @@ -5,6 +5,7 @@ using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Runtime.Intrinsics; +using System.Runtime.Intrinsics.Arm; using System.Runtime.Intrinsics.X86; namespace SixLabors.ImageSharp.Formats.Png.Filters; @@ -34,6 +35,10 @@ internal static class UpFilter { DecodeSse2(scanline, previousScanline); } + else if (AdvSimd.IsSupported) + { + DecodeArm(scanline, previousScanline); + } else { DecodeScalar(scanline, previousScanline); @@ -51,11 +56,10 @@ internal static class UpFilter while (rb >= Vector256.Count) { ref byte scanRef = ref Unsafe.Add(ref scanBaseRef, offset); - Vector256 current = Unsafe.As>(ref scanRef); + Vector256 prior = Unsafe.As>(ref scanRef); Vector256 up = Unsafe.As>(ref Unsafe.Add(ref prevBaseRef, offset)); - Vector256 sum = Avx2.Add(up, current); - Unsafe.As>(ref scanRef) = sum; + Unsafe.As>(ref scanRef) = Avx2.Add(up, prior); offset += Vector256.Count; rb -= Vector256.Count; @@ -82,11 +86,10 @@ internal static class UpFilter while (rb >= Vector128.Count) { ref byte scanRef = ref Unsafe.Add(ref scanBaseRef, offset); - Vector128 current = Unsafe.As>(ref scanRef); + Vector128 prior = Unsafe.As>(ref scanRef); Vector128 up = Unsafe.As>(ref Unsafe.Add(ref prevBaseRef, offset)); - Vector128 sum = Sse2.Add(up, current); - Unsafe.As>(ref scanRef) = sum; + Unsafe.As>(ref scanRef) = Sse2.Add(up, prior); offset += Vector128.Count; rb -= Vector128.Count; @@ -102,6 +105,37 @@ internal static class UpFilter } } + private static void DecodeArm(Span scanline, Span previousScanline) + { + ref byte scanBaseRef = ref MemoryMarshal.GetReference(scanline); + ref byte prevBaseRef = ref MemoryMarshal.GetReference(previousScanline); + + // Up(x) + Prior(x) + int rb = scanline.Length; + nint offset = 1; + const int bytesPerBatch = 16; + while (rb >= bytesPerBatch) + { + ref byte scanRef = ref Unsafe.Add(ref scanBaseRef, offset); + Vector128 prior = Unsafe.As>(ref scanRef); + Vector128 up = Unsafe.As>(ref Unsafe.Add(ref prevBaseRef, offset)); + + Unsafe.As>(ref scanRef) = AdvSimd.Add(prior, up); + + offset += bytesPerBatch; + rb -= bytesPerBatch; + } + + // Handle left over. + for (nint i = offset; i < scanline.Length; i++) + { + ref byte scan = ref Unsafe.Add(ref scanBaseRef, offset); + byte above = Unsafe.Add(ref prevBaseRef, offset); + scan = (byte)(scan + above); + offset++; + } + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] private static void DecodeScalar(Span scanline, Span previousScanline) { diff --git a/tests/ImageSharp.Tests/Formats/Png/Crc32Tests.cs b/tests/ImageSharp.Tests/Formats/Png/Crc32Tests.cs index 0dea05c531..ff91590f95 100644 --- a/tests/ImageSharp.Tests/Formats/Png/Crc32Tests.cs +++ b/tests/ImageSharp.Tests/Formats/Png/Crc32Tests.cs @@ -2,6 +2,7 @@ // Licensed under the Six Labors Split License. using SixLabors.ImageSharp.Compression.Zlib; +using SixLabors.ImageSharp.Tests.TestUtilities; using SharpCrc32 = ICSharpCode.SharpZipLib.Checksum.Crc32; namespace SixLabors.ImageSharp.Tests.Formats.Png; @@ -13,10 +14,7 @@ public class Crc32Tests [InlineData(0)] [InlineData(1)] [InlineData(2)] - public void ReturnsCorrectWhenEmpty(uint input) - { - Assert.Equal(input, Crc32.Calculate(input, default)); - } + public void CalculateCrc_ReturnsCorrectResultWhenEmpty(uint input) => Assert.Equal(input, Crc32.Calculate(input, default)); [Theory] [InlineData(0)] @@ -26,23 +24,43 @@ public class Crc32Tests [InlineData(1024 + 15)] [InlineData(2034)] [InlineData(4096)] - public void MatchesReference(int length) + public void CalculateCrc_MatchesReference(int length) => CalculateCrcAndCompareToReference(length); + + private static void CalculateCrcAndCompareToReference(int length) { - var data = GetBuffer(length); - var crc = new SharpCrc32(); + // arrange + byte[] data = GetBuffer(length); + SharpCrc32 crc = new(); crc.Update(data); - long expected = crc.Value; + + // act long actual = Crc32.Calculate(data); + // assert Assert.Equal(expected, actual); } private static byte[] GetBuffer(int length) { - var data = new byte[length]; + byte[] data = new byte[length]; new Random(1).NextBytes(data); return data; } + + [Fact] + public void RunCalculateCrcTest_WithHardwareIntrinsics_Works() => FeatureTestRunner.RunWithHwIntrinsicsFeature(RunCalculateCrcTest, HwIntrinsics.AllowAll); + + [Fact] + public void RunCalculateCrcTest_WithoutHardwareIntrinsics_Works() => FeatureTestRunner.RunWithHwIntrinsicsFeature(RunCalculateCrcTest, HwIntrinsics.DisableHWIntrinsic); + + private static void RunCalculateCrcTest() + { + int[] testData = { 0, 8, 215, 1024, 1024 + 15, 2034, 4096 }; + for (int i = 0; i < testData.Length; i++) + { + CalculateCrcAndCompareToReference(testData[i]); + } + } } diff --git a/tests/ImageSharp.Tests/TestUtilities/FeatureTesting/FeatureTestRunner.cs b/tests/ImageSharp.Tests/TestUtilities/FeatureTesting/FeatureTestRunner.cs index ad5aa4769f..1bb64d99d6 100644 --- a/tests/ImageSharp.Tests/TestUtilities/FeatureTesting/FeatureTestRunner.cs +++ b/tests/ImageSharp.Tests/TestUtilities/FeatureTesting/FeatureTestRunner.cs @@ -12,7 +12,7 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities; /// public static class FeatureTestRunner { - private static readonly char[] SplitChars = new[] { ',', ' ' }; + private static readonly char[] SplitChars = { ',', ' ' }; /// /// Allows the deserialization of parameters passed to the feature test. @@ -35,6 +35,7 @@ public static class FeatureTestRunner /// Allows the deserialization of types implementing /// passed to the feature test. /// + /// The type of object to deserialize. /// The string value to deserialize. /// The value. public static T Deserialize(string value) @@ -58,7 +59,7 @@ public static class FeatureTestRunner foreach (KeyValuePair intrinsic in intrinsics.ToFeatureKeyValueCollection()) { - var processStartInfo = new ProcessStartInfo(); + ProcessStartInfo processStartInfo = new(); if (intrinsic.Key != HwIntrinsics.AllowAll) { processStartInfo.Environment[$"COMPlus_{intrinsic.Value}"] = "0"; @@ -99,7 +100,7 @@ public static class FeatureTestRunner foreach (KeyValuePair intrinsic in intrinsics.ToFeatureKeyValueCollection()) { - var processStartInfo = new ProcessStartInfo(); + ProcessStartInfo processStartInfo = new(); if (intrinsic.Key != HwIntrinsics.AllowAll) { processStartInfo.Environment[$"COMPlus_{intrinsic.Value}"] = "0"; @@ -142,7 +143,7 @@ public static class FeatureTestRunner foreach (KeyValuePair intrinsic in intrinsics.ToFeatureKeyValueCollection()) { - var processStartInfo = new ProcessStartInfo(); + ProcessStartInfo processStartInfo = new(); if (intrinsic.Key != HwIntrinsics.AllowAll) { processStartInfo.Environment[$"COMPlus_{intrinsic.Value}"] = "0"; @@ -185,7 +186,7 @@ public static class FeatureTestRunner foreach (KeyValuePair intrinsic in intrinsics.ToFeatureKeyValueCollection()) { - var processStartInfo = new ProcessStartInfo(); + ProcessStartInfo processStartInfo = new(); if (intrinsic.Key != HwIntrinsics.AllowAll) { processStartInfo.Environment[$"COMPlus_{intrinsic.Value}"] = "0"; @@ -232,7 +233,7 @@ public static class FeatureTestRunner foreach (KeyValuePair intrinsic in intrinsics.ToFeatureKeyValueCollection()) { - var processStartInfo = new ProcessStartInfo(); + ProcessStartInfo processStartInfo = new(); if (intrinsic.Key != HwIntrinsics.AllowAll) { processStartInfo.Environment[$"COMPlus_{intrinsic.Value}"] = "0"; @@ -276,7 +277,7 @@ public static class FeatureTestRunner foreach (KeyValuePair intrinsic in intrinsics.ToFeatureKeyValueCollection()) { - var processStartInfo = new ProcessStartInfo(); + ProcessStartInfo processStartInfo = new(); if (intrinsic.Key != HwIntrinsics.AllowAll) { processStartInfo.Environment[$"COMPlus_{intrinsic.Value}"] = "0"; @@ -321,7 +322,7 @@ public static class FeatureTestRunner foreach (KeyValuePair intrinsic in intrinsics.ToFeatureKeyValueCollection()) { - var processStartInfo = new ProcessStartInfo(); + ProcessStartInfo processStartInfo = new(); if (intrinsic.Key != HwIntrinsics.AllowAll) { processStartInfo.Environment[$"COMPlus_{intrinsic.Value}"] = "0"; @@ -347,11 +348,11 @@ public static class FeatureTestRunner internal static Dictionary ToFeatureKeyValueCollection(this HwIntrinsics intrinsics) { - // Loop through and translate the given values into COMPlus equivaluents - var features = new Dictionary(); + // Loop through and translate the given values into COMPlus equivalents + Dictionary features = new(); foreach (string intrinsic in intrinsics.ToString("G").Split(SplitChars, StringSplitOptions.RemoveEmptyEntries)) { - var key = (HwIntrinsics)Enum.Parse(typeof(HwIntrinsics), intrinsic); + HwIntrinsics key = (HwIntrinsics)Enum.Parse(typeof(HwIntrinsics), intrinsic); switch (intrinsic) { case nameof(HwIntrinsics.AllowAll): @@ -400,5 +401,11 @@ public enum HwIntrinsics DisableBMI1 = 1 << 13, DisableBMI2 = 1 << 14, DisableLZCNT = 1 << 15, - AllowAll = 1 << 16 + DisableArm64AdvSimd = 1 << 16, + DisableArm64Crc32 = 1 << 17, + DisableArm64Dp = 1 << 18, + DisableArm64Aes = 1 << 19, + DisableArm64Sha1 = 1 << 20, + DisableArm64Sha256 = 1 << 21, + AllowAll = 1 << 22 } diff --git a/tests/ImageSharp.Tests/TestUtilities/Tests/FeatureTestRunnerTests.cs b/tests/ImageSharp.Tests/TestUtilities/Tests/FeatureTestRunnerTests.cs index 6ce07e7665..34337600eb 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Tests/FeatureTestRunnerTests.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Tests/FeatureTestRunnerTests.cs @@ -2,8 +2,10 @@ // Licensed under the Six Labors Split License. using System.Numerics; +using System.Runtime.Intrinsics.Arm; using System.Runtime.Intrinsics.X86; using Xunit.Abstractions; +using Aes = System.Runtime.Intrinsics.X86.Aes; namespace SixLabors.ImageSharp.Tests.TestUtilities.Tests; @@ -12,9 +14,9 @@ public class FeatureTestRunnerTests public static TheoryData Intrinsics => new() { - { HwIntrinsics.DisableAES | HwIntrinsics.AllowAll, new string[] { "EnableAES", "AllowAll" } }, - { HwIntrinsics.DisableHWIntrinsic, new string[] { "EnableHWIntrinsic" } }, - { HwIntrinsics.DisableSSE42 | HwIntrinsics.DisableAVX, new string[] { "EnableSSE42", "EnableAVX" } } + { HwIntrinsics.DisableAES | HwIntrinsics.AllowAll, new[] { "EnableAES", "AllowAll" } }, + { HwIntrinsics.DisableHWIntrinsic, new[] { "EnableHWIntrinsic" } }, + { HwIntrinsics.DisableSSE42 | HwIntrinsics.DisableAVX, new[] { "EnableSSE42", "EnableAVX" } } }; [Theory] @@ -101,6 +103,12 @@ public class FeatureTestRunnerTests Assert.False(Bmi1.IsSupported); Assert.False(Bmi2.IsSupported); Assert.False(Lzcnt.IsSupported); + Assert.False(AdvSimd.IsSupported); + Assert.False(System.Runtime.Intrinsics.Arm.Aes.IsSupported); + Assert.False(Crc32.IsSupported); + Assert.False(Dp.IsSupported); + Assert.False(Sha1.IsSupported); + Assert.False(Sha256.IsSupported); break; case HwIntrinsics.DisableSSE: Assert.False(Sse.IsSupported); @@ -147,6 +155,24 @@ public class FeatureTestRunnerTests case HwIntrinsics.DisableLZCNT: Assert.False(Lzcnt.IsSupported); break; + case HwIntrinsics.DisableArm64AdvSimd: + Assert.False(AdvSimd.IsSupported); + break; + case HwIntrinsics.DisableArm64Aes: + Assert.False(System.Runtime.Intrinsics.Arm.Aes.IsSupported); + break; + case HwIntrinsics.DisableArm64Crc32: + Assert.False(Crc32.IsSupported); + break; + case HwIntrinsics.DisableArm64Dp: + Assert.False(Dp.IsSupported); + break; + case HwIntrinsics.DisableArm64Sha1: + Assert.False(Sha1.IsSupported); + break; + case HwIntrinsics.DisableArm64Sha256: + Assert.False(Sha256.IsSupported); + break; } } @@ -198,6 +224,12 @@ public class FeatureTestRunnerTests Assert.False(Bmi1.IsSupported); Assert.False(Bmi2.IsSupported); Assert.False(Lzcnt.IsSupported); + Assert.False(AdvSimd.IsSupported); + Assert.False(System.Runtime.Intrinsics.Arm.Aes.IsSupported); + Assert.False(Crc32.IsSupported); + Assert.False(Dp.IsSupported); + Assert.False(Sha1.IsSupported); + Assert.False(Sha256.IsSupported); break; case HwIntrinsics.DisableSSE: Assert.False(Sse.IsSupported); @@ -244,6 +276,24 @@ public class FeatureTestRunnerTests case HwIntrinsics.DisableLZCNT: Assert.False(Lzcnt.IsSupported); break; + case HwIntrinsics.DisableArm64AdvSimd: + Assert.False(AdvSimd.IsSupported); + break; + case HwIntrinsics.DisableArm64Aes: + Assert.False(System.Runtime.Intrinsics.Arm.Aes.IsSupported); + break; + case HwIntrinsics.DisableArm64Crc32: + Assert.False(Crc32.IsSupported); + break; + case HwIntrinsics.DisableArm64Dp: + Assert.False(Dp.IsSupported); + break; + case HwIntrinsics.DisableArm64Sha1: + Assert.False(Sha1.IsSupported); + break; + case HwIntrinsics.DisableArm64Sha256: + Assert.False(Sha256.IsSupported); + break; } } From 3182f935dc84b4ebfce2b506ab0d5eb83ca8aa5a Mon Sep 17 00:00:00 2001 From: Stefan Nikolei Date: Sun, 5 Feb 2023 15:25:49 +0100 Subject: [PATCH 2/9] Remove nullable disable from Formats.BMP * Removed nullable disable * refactored stream usage. It is now always passed as a parameter and not stored a a instance variable --- src/ImageSharp/Color/Color.cs | 3 +- src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs | 179 ++++++++++-------- src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs | 5 +- .../PaletteDitherProcessor{TPixel}.cs | 2 +- .../Processors/Quantization/IQuantizer.cs | 4 +- .../Quantization/OctreeQuantizer.cs | 4 +- .../Quantization/OctreeQuantizer{TPixel}.cs | 2 +- .../Quantization/PaletteQuantizer.cs | 6 +- .../Quantization/PaletteQuantizer{TPixel}.cs | 2 +- .../Processors/Quantization/WuQuantizer.cs | 4 +- .../Quantization/WuQuantizer{TPixel}.cs | 2 +- 11 files changed, 112 insertions(+), 101 deletions(-) diff --git a/src/ImageSharp/Color/Color.cs b/src/ImageSharp/Color/Color.cs index 99a271bcf7..590a3ed311 100644 --- a/src/ImageSharp/Color/Color.cs +++ b/src/ImageSharp/Color/Color.cs @@ -286,12 +286,11 @@ public readonly partial struct Color : IEquatable /// Bulk converts a span of to a span of a specified type. /// /// The pixel type to convert to. - /// The configuration. /// The source color span. /// The destination pixel span. [MethodImpl(InliningOptions.ShortMethod)] #pragma warning disable RCS1163 // Unused parameter. - public static void ToPixel(Configuration configuration, ReadOnlySpan source, Span destination) + public static void ToPixel(ReadOnlySpan source, Span destination) #pragma warning restore RCS1163 // Unused parameter. where TPixel : unmanaged, IPixel { diff --git a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs index fd0497d9ac..cb1c7a2511 100644 --- a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs @@ -1,9 +1,9 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. -#nullable disable using System.Buffers; using System.Buffers.Binary; +using System.Diagnostics.CodeAnalysis; using System.Numerics; using System.Runtime.CompilerServices; using SixLabors.ImageSharp.Common.Helpers; @@ -58,20 +58,15 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals /// private const int RleDelta = 0x02; - /// - /// The stream to decode from. - /// - private BufferedReadStream stream; - /// /// The metadata. /// - private ImageMetadata metadata; + private ImageMetadata? metadata; /// /// The bitmap specific metadata. /// - private BmpMetadata bmpMetadata; + private BmpMetadata? bmpMetadata; /// /// The file header containing general information. @@ -126,7 +121,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals public Image Decode(BufferedReadStream stream, CancellationToken cancellationToken) where TPixel : unmanaged, IPixel { - Image image = null; + Image? image = null; try { int bytesPerColorMapEntry = this.ReadImageHeaders(stream, out bool inverted, out byte[] palette); @@ -142,24 +137,25 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals { if (this.bmpMetadata.InfoHeaderType == BmpInfoHeaderType.WinVersion3) { - this.ReadRgb32Slow(pixels, this.infoHeader.Width, this.infoHeader.Height, inverted); + this.ReadRgb32Slow(stream, pixels, this.infoHeader.Width, this.infoHeader.Height, inverted); } else { - this.ReadRgb32Fast(pixels, this.infoHeader.Width, this.infoHeader.Height, inverted); + this.ReadRgb32Fast(stream, pixels, this.infoHeader.Width, this.infoHeader.Height, inverted); } } else if (this.infoHeader.BitsPerPixel == 24) { - this.ReadRgb24(pixels, this.infoHeader.Width, this.infoHeader.Height, inverted); + this.ReadRgb24(stream, pixels, this.infoHeader.Width, this.infoHeader.Height, inverted); } else if (this.infoHeader.BitsPerPixel == 16) { - this.ReadRgb16(pixels, this.infoHeader.Width, this.infoHeader.Height, inverted); + this.ReadRgb16(stream, pixels, this.infoHeader.Width, this.infoHeader.Height, inverted); } else if (this.infoHeader.BitsPerPixel <= 8) { this.ReadRgbPalette( + stream, pixels, palette, this.infoHeader.Width, @@ -172,19 +168,19 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals break; case BmpCompression.RLE24: - this.ReadRle24(pixels, this.infoHeader.Width, this.infoHeader.Height, inverted); + this.ReadRle24(stream, pixels, this.infoHeader.Width, this.infoHeader.Height, inverted); break; case BmpCompression.RLE8: case BmpCompression.RLE4: - this.ReadRle(this.infoHeader.Compression, pixels, palette, this.infoHeader.Width, this.infoHeader.Height, inverted); + this.ReadRle(stream, this.infoHeader.Compression, pixels, palette, this.infoHeader.Width, this.infoHeader.Height, inverted); break; case BmpCompression.BitFields: case BmpCompression.BI_ALPHABITFIELDS: - this.ReadBitFields(pixels, inverted); + this.ReadBitFields(stream, pixels, inverted); break; @@ -250,14 +246,16 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals /// which will be used to determine which bits belong to that channel. /// /// The pixel format. + /// The containing image data. /// The output pixel buffer containing the decoded image. /// Whether the bitmap is inverted. - private void ReadBitFields(Buffer2D pixels, bool inverted) + private void ReadBitFields(BufferedReadStream stream, Buffer2D pixels, bool inverted) where TPixel : unmanaged, IPixel { if (this.infoHeader.BitsPerPixel == 16) { this.ReadRgb16( + stream, pixels, this.infoHeader.Width, this.infoHeader.Height, @@ -269,6 +267,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals else { this.ReadRgb32BitFields( + stream, pixels, this.infoHeader.Width, this.infoHeader.Height, @@ -282,17 +281,17 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals /// /// Looks up color values and builds the image from de-compressed RLE8 or RLE4 data. - /// Compressed RLE8 stream is uncompressed by - /// Compressed RLE4 stream is uncompressed by + /// Compressed RLE4 stream is uncompressed by /// /// The pixel format. + /// The containing image data. /// The compression type. Either RLE4 or RLE8. /// The to assign the palette to. /// The containing the colors. /// The width of the bitmap. /// The height of the bitmap. /// Whether the bitmap is inverted. - private void ReadRle(BmpCompression compression, Buffer2D pixels, byte[] colors, int width, int height, bool inverted) + private void ReadRle(BufferedReadStream stream, BmpCompression compression, Buffer2D pixels, byte[] colors, int width, int height, bool inverted) where TPixel : unmanaged, IPixel { TPixel color = default; @@ -305,11 +304,11 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals Span bufferSpan = buffer.Memory.Span; if (compression is BmpCompression.RLE8) { - this.UncompressRle8(width, bufferSpan, undefinedPixelsSpan, rowsWithUndefinedPixelsSpan); + this.UncompressRle8(stream, width, bufferSpan, undefinedPixelsSpan, rowsWithUndefinedPixelsSpan); } else { - this.UncompressRle4(width, bufferSpan, undefinedPixelsSpan, rowsWithUndefinedPixelsSpan); + this.UncompressRle4(stream, width, bufferSpan, undefinedPixelsSpan, rowsWithUndefinedPixelsSpan); } for (int y = 0; y < height; y++) @@ -368,11 +367,12 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals /// Looks up color values and builds the image from de-compressed RLE24. /// /// The pixel format. + /// The containing image data. /// The to assign the palette to. /// The width of the bitmap. /// The height of the bitmap. /// Whether the bitmap is inverted. - private void ReadRle24(Buffer2D pixels, int width, int height, bool inverted) + private void ReadRle24(BufferedReadStream stream, Buffer2D pixels, int width, int height, bool inverted) where TPixel : unmanaged, IPixel { TPixel color = default; @@ -384,7 +384,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals Span undefinedPixelsSpan = undefinedPixels.Memory.Span; Span bufferSpan = buffer.GetSpan(); - this.UncompressRle24(width, bufferSpan, undefinedPixelsSpan, rowsWithUndefinedPixelsSpan); + this.UncompressRle24(stream, width, bufferSpan, undefinedPixelsSpan, rowsWithUndefinedPixelsSpan); for (int y = 0; y < height; y++) { int newY = Invert(y, height, inverted); @@ -446,18 +446,19 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals ///
If first byte is 0, the second byte may have special meaning. ///
Otherwise, the first byte is the length of the run and second byte contains two color indexes. /// + /// The containing image data. /// The width of the bitmap. /// Buffer for uncompressed data. /// Keeps track over skipped and therefore undefined pixels. /// Keeps track of rows, which have undefined pixels. - private void UncompressRle4(int w, Span buffer, Span undefinedPixels, Span rowsWithUndefinedPixels) + private void UncompressRle4(BufferedReadStream stream, int w, Span buffer, Span undefinedPixels, Span rowsWithUndefinedPixels) { Span cmd = stackalloc byte[2]; int count = 0; while (count < buffer.Length) { - if (this.stream.Read(cmd, 0, cmd.Length) != 2) + if (stream.Read(cmd, 0, cmd.Length) != 2) { BmpThrowHelper.ThrowInvalidImageContentException("Failed to read 2 bytes from the stream while uncompressing RLE4 bitmap."); } @@ -478,8 +479,8 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals break; case RleDelta: - int dx = this.stream.ReadByte(); - int dy = this.stream.ReadByte(); + int dx = stream.ReadByte(); + int dy = stream.ReadByte(); count += RleSkipDelta(count, w, dx, dy, undefinedPixels, rowsWithUndefinedPixels); break; @@ -492,7 +493,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals byte[] run = new byte[bytesToRead]; - this.stream.Read(run, 0, run.Length); + stream.Read(run, 0, run.Length); int idx = 0; for (int i = 0; i < max; i++) @@ -512,7 +513,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals // Absolute mode data is aligned to two-byte word-boundary. int padding = bytesToRead & 1; - this.stream.Skip(padding); + stream.Skip(padding); break; } @@ -551,18 +552,19 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals ///
If first byte is 0, the second byte may have special meaning. ///
Otherwise, the first byte is the length of the run and second byte is the color for the run. /// + /// The containing image data. /// The width of the bitmap. /// Buffer for uncompressed data. /// Keeps track of skipped and therefore undefined pixels. /// Keeps track of rows, which have undefined pixels. - private void UncompressRle8(int w, Span buffer, Span undefinedPixels, Span rowsWithUndefinedPixels) + private void UncompressRle8(BufferedReadStream stream, int w, Span buffer, Span undefinedPixels, Span rowsWithUndefinedPixels) { Span cmd = stackalloc byte[2]; int count = 0; while (count < buffer.Length) { - if (this.stream.Read(cmd, 0, cmd.Length) != 2) + if (stream.Read(cmd, 0, cmd.Length) != 2) { BmpThrowHelper.ThrowInvalidImageContentException("Failed to read 2 bytes from stream while uncompressing RLE8 bitmap."); } @@ -583,8 +585,8 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals break; case RleDelta: - int dx = this.stream.ReadByte(); - int dy = this.stream.ReadByte(); + int dx = stream.ReadByte(); + int dy = stream.ReadByte(); count += RleSkipDelta(count, w, dx, dy, undefinedPixels, rowsWithUndefinedPixels); break; @@ -596,7 +598,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals byte[] run = new byte[length]; - this.stream.Read(run, 0, run.Length); + stream.Read(run, 0, run.Length); run.AsSpan().CopyTo(buffer[count..]); @@ -605,7 +607,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals // Absolute mode data is aligned to two-byte word-boundary. int padding = length & 1; - this.stream.Skip(padding); + stream.Skip(padding); break; } @@ -630,18 +632,19 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals ///
If first byte is 0, the second byte may have special meaning. ///
Otherwise, the first byte is the length of the run and following three bytes are the color for the run. /// + /// The containing image data. /// The width of the bitmap. /// Buffer for uncompressed data. /// Keeps track of skipped and therefore undefined pixels. /// Keeps track of rows, which have undefined pixels. - private void UncompressRle24(int w, Span buffer, Span undefinedPixels, Span rowsWithUndefinedPixels) + private void UncompressRle24(BufferedReadStream stream, int w, Span buffer, Span undefinedPixels, Span rowsWithUndefinedPixels) { Span cmd = stackalloc byte[2]; int uncompressedPixels = 0; while (uncompressedPixels < buffer.Length) { - if (this.stream.Read(cmd, 0, cmd.Length) != 2) + if (stream.Read(cmd, 0, cmd.Length) != 2) { BmpThrowHelper.ThrowInvalidImageContentException("Failed to read 2 bytes from stream while uncompressing RLE24 bitmap."); } @@ -662,8 +665,8 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals break; case RleDelta: - int dx = this.stream.ReadByte(); - int dy = this.stream.ReadByte(); + int dx = stream.ReadByte(); + int dy = stream.ReadByte(); uncompressedPixels += RleSkipDelta(uncompressedPixels, w, dx, dy, undefinedPixels, rowsWithUndefinedPixels); break; @@ -675,7 +678,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals byte[] run = new byte[length * 3]; - this.stream.Read(run, 0, run.Length); + stream.Read(run, 0, run.Length); run.AsSpan().CopyTo(buffer[(uncompressedPixels * 3)..]); @@ -684,7 +687,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals // Absolute mode data is aligned to two-byte word-boundary. int padding = run.Length & 1; - this.stream.Skip(padding); + stream.Skip(padding); break; } @@ -693,8 +696,8 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals { int max = uncompressedPixels + cmd[0]; byte blueIdx = cmd[1]; - byte greenIdx = (byte)this.stream.ReadByte(); - byte redIdx = (byte)this.stream.ReadByte(); + byte greenIdx = (byte)stream.ReadByte(); + byte redIdx = (byte)stream.ReadByte(); int bufferIdx = uncompressedPixels * 3; for (; uncompressedPixels < max; uncompressedPixels++) @@ -800,6 +803,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals /// Reads the color palette from the stream. /// /// The pixel format. + /// The containing image data. /// The to assign the palette to. /// The containing the colors. /// The width of the bitmap. @@ -808,7 +812,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals /// Usually 4 bytes, but in case of Windows 2.x bitmaps or OS/2 1.x bitmaps /// the bytes per color palette entry's can be 3 bytes instead of 4. /// Whether the bitmap is inverted. - private void ReadRgbPalette(Buffer2D pixels, byte[] colors, int width, int height, int bitsPerPixel, int bytesPerColorMapEntry, bool inverted) + private void ReadRgbPalette(BufferedReadStream stream, Buffer2D pixels, byte[] colors, int width, int height, int bitsPerPixel, int bytesPerColorMapEntry, bool inverted) where TPixel : unmanaged, IPixel { // Pixels per byte (bits per pixel). @@ -833,7 +837,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals for (int y = 0; y < height; y++) { int newY = Invert(y, height, inverted); - if (this.stream.Read(rowSpan) == 0) + if (stream.Read(rowSpan) == 0) { BmpThrowHelper.ThrowInvalidImageContentException("Could not read enough data for a pixel row!"); } @@ -861,6 +865,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals /// Reads the 16 bit color palette from the stream. /// /// The pixel format. + /// The containing image data. /// The to assign the palette to. /// The width of the bitmap. /// The height of the bitmap. @@ -868,7 +873,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals /// The bitmask for the red channel. /// The bitmask for the green channel. /// The bitmask for the blue channel. - private void ReadRgb16(Buffer2D pixels, int width, int height, bool inverted, int redMask = DefaultRgb16RMask, int greenMask = DefaultRgb16GMask, int blueMask = DefaultRgb16BMask) + private void ReadRgb16(BufferedReadStream stream, Buffer2D pixels, int width, int height, bool inverted, int redMask = DefaultRgb16RMask, int greenMask = DefaultRgb16GMask, int blueMask = DefaultRgb16BMask) where TPixel : unmanaged, IPixel { int padding = CalculatePadding(width, 2); @@ -889,7 +894,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals for (int y = 0; y < height; y++) { - if (this.stream.Read(bufferSpan) == 0) + if (stream.Read(bufferSpan) == 0) { BmpThrowHelper.ThrowInvalidImageContentException("Could not read enough data for a pixel row!"); } @@ -935,11 +940,12 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals /// Reads the 24 bit color palette from the stream. /// /// The pixel format. + /// The containing image data. /// The to assign the palette to. /// The width of the bitmap. /// The height of the bitmap. /// Whether the bitmap is inverted. - private void ReadRgb24(Buffer2D pixels, int width, int height, bool inverted) + private void ReadRgb24(BufferedReadStream stream, Buffer2D pixels, int width, int height, bool inverted) where TPixel : unmanaged, IPixel { int padding = CalculatePadding(width, 3); @@ -948,7 +954,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals for (int y = 0; y < height; y++) { - if (this.stream.Read(rowSpan) == 0) + if (stream.Read(rowSpan) == 0) { BmpThrowHelper.ThrowInvalidImageContentException("Could not read enough data for a pixel row!"); } @@ -967,11 +973,12 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals /// Reads the 32 bit color palette from the stream. /// /// The pixel format. + /// The containing image data. /// The to assign the palette to. /// The width of the bitmap. /// The height of the bitmap. /// Whether the bitmap is inverted. - private void ReadRgb32Fast(Buffer2D pixels, int width, int height, bool inverted) + private void ReadRgb32Fast(BufferedReadStream stream, Buffer2D pixels, int width, int height, bool inverted) where TPixel : unmanaged, IPixel { int padding = CalculatePadding(width, 4); @@ -980,7 +987,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals for (int y = 0; y < height; y++) { - if (this.stream.Read(rowSpan) == 0) + if (stream.Read(rowSpan) == 0) { BmpThrowHelper.ThrowInvalidImageContentException("Could not read enough data for a pixel row!"); } @@ -1000,11 +1007,12 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals /// This is a special case only used for 32bpp WinBMPv3 files, which could be in either BGR0 or BGRA format. /// /// The pixel format. + /// The containing image data. /// The to assign the palette to. /// The width of the bitmap. /// The height of the bitmap. /// Whether the bitmap is inverted. - private void ReadRgb32Slow(Buffer2D pixels, int width, int height, bool inverted) + private void ReadRgb32Slow(BufferedReadStream stream, Buffer2D pixels, int width, int height, bool inverted) where TPixel : unmanaged, IPixel { int padding = CalculatePadding(width, 4); @@ -1012,7 +1020,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals using IMemoryOwner bgraRow = this.memoryAllocator.Allocate(width); Span rowSpan = row.GetSpan(); Span bgraRowSpan = bgraRow.GetSpan(); - long currentPosition = this.stream.Position; + long currentPosition = stream.Position; bool hasAlpha = false; // Loop though the rows checking each pixel. We start by assuming it's @@ -1020,7 +1028,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals // actually a BGRA image, and change tactics accordingly. for (int y = 0; y < height; y++) { - if (this.stream.Read(rowSpan) == 0) + if (stream.Read(rowSpan) == 0) { BmpThrowHelper.ThrowInvalidImageContentException("Could not read enough data for a pixel row!"); } @@ -1049,14 +1057,14 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals } // Reset our stream for a second pass. - this.stream.Position = currentPosition; + stream.Position = currentPosition; // Process the pixels in bulk taking the raw alpha component value. if (hasAlpha) { for (int y = 0; y < height; y++) { - if (this.stream.Read(rowSpan) == 0) + if (stream.Read(rowSpan) == 0) { BmpThrowHelper.ThrowInvalidImageContentException("Could not read enough data for a pixel row!"); } @@ -1077,7 +1085,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals // Slow path. We need to set each alpha component value to fully opaque. for (int y = 0; y < height; y++) { - if (this.stream.Read(rowSpan) == 0) + if (stream.Read(rowSpan) == 0) { BmpThrowHelper.ThrowInvalidImageContentException("Could not read enough data for a pixel row!"); } @@ -1105,6 +1113,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals /// Decode an 32 Bit Bitmap containing a bitmask for each color channel. /// /// The pixel format. + /// The containing image data. /// The output pixel buffer containing the decoded image. /// The width of the image. /// The height of the image. @@ -1113,7 +1122,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals /// The bitmask for the green channel. /// The bitmask for the blue channel. /// The bitmask for the alpha channel. - private void ReadRgb32BitFields(Buffer2D pixels, int width, int height, bool inverted, int redMask, int greenMask, int blueMask, int alphaMask) + private void ReadRgb32BitFields(BufferedReadStream stream, Buffer2D pixels, int width, int height, bool inverted, int redMask, int greenMask, int blueMask, int alphaMask) where TPixel : unmanaged, IPixel { TPixel color = default; @@ -1142,7 +1151,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals for (int y = 0; y < height; y++) { - if (this.stream.Read(bufferSpan) == 0) + if (stream.Read(bufferSpan) == 0) { BmpThrowHelper.ThrowInvalidImageContentException("Could not read enough data for a pixel row!"); } @@ -1228,10 +1237,13 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals /// /// Reads the from the stream. /// - private void ReadInfoHeader() + /// The containing image data. + [MemberNotNull(nameof(metadata))] + [MemberNotNull(nameof(bmpMetadata))] + private void ReadInfoHeader(BufferedReadStream stream) { Span buffer = stackalloc byte[BmpInfoHeader.MaxHeaderSize]; - long infoHeaderStart = this.stream.Position; + long infoHeaderStart = stream.Position; // Resolution is stored in PPM. this.metadata = new ImageMetadata @@ -1240,7 +1252,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals }; // Read the header size. - this.stream.Read(buffer, 0, BmpInfoHeader.HeaderSizeSize); + stream.Read(buffer, 0, BmpInfoHeader.HeaderSizeSize); int headerSize = BinaryPrimitives.ReadInt32LittleEndian(buffer); if (headerSize is < BmpInfoHeader.CoreSize or > BmpInfoHeader.MaxHeaderSize) @@ -1249,7 +1261,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals } // Read the rest of the header. - this.stream.Read(buffer, BmpInfoHeader.HeaderSizeSize, headerSize - BmpInfoHeader.HeaderSizeSize); + stream.Read(buffer, BmpInfoHeader.HeaderSizeSize, headerSize - BmpInfoHeader.HeaderSizeSize); BmpInfoHeaderType infoHeaderType = BmpInfoHeaderType.WinVersion2; if (headerSize == BmpInfoHeader.CoreSize) @@ -1275,7 +1287,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals if (this.infoHeader.Compression == BmpCompression.BitFields) { byte[] bitfieldsBuffer = new byte[12]; - this.stream.Read(bitfieldsBuffer, 0, 12); + stream.Read(bitfieldsBuffer, 0, 12); Span data = bitfieldsBuffer.AsSpan(); this.infoHeader.RedMask = BinaryPrimitives.ReadInt32LittleEndian(data[..4]); this.infoHeader.GreenMask = BinaryPrimitives.ReadInt32LittleEndian(data.Slice(4, 4)); @@ -1284,7 +1296,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals else if (this.infoHeader.Compression == BmpCompression.BI_ALPHABITFIELDS) { byte[] bitfieldsBuffer = new byte[16]; - this.stream.Read(bitfieldsBuffer, 0, 16); + stream.Read(bitfieldsBuffer, 0, 16); Span data = bitfieldsBuffer.AsSpan(); this.infoHeader.RedMask = BinaryPrimitives.ReadInt32LittleEndian(data[..4]); this.infoHeader.GreenMask = BinaryPrimitives.ReadInt32LittleEndian(data.Slice(4, 4)); @@ -1324,12 +1336,12 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals if (this.infoHeader.ProfileData != 0 && this.infoHeader.ProfileSize != 0) { // Read color profile. - long streamPosition = this.stream.Position; + long streamPosition = stream.Position; byte[] iccProfileData = new byte[this.infoHeader.ProfileSize]; - this.stream.Position = infoHeaderStart + this.infoHeader.ProfileData; - this.stream.Read(iccProfileData); + stream.Position = infoHeaderStart + this.infoHeader.ProfileData; + stream.Read(iccProfileData); this.metadata.IccProfile = new IccProfile(iccProfileData); - this.stream.Position = streamPosition; + stream.Position = streamPosition; } } else @@ -1358,10 +1370,11 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals /// /// Reads the from the stream. /// - private void ReadFileHeader() + /// The containing image data. + private void ReadFileHeader(BufferedReadStream stream) { Span buffer = stackalloc byte[BmpFileHeader.Size]; - this.stream.Read(buffer, 0, BmpFileHeader.Size); + stream.Read(buffer, 0, BmpFileHeader.Size); short fileTypeMarker = BinaryPrimitives.ReadInt16LittleEndian(buffer); switch (fileTypeMarker) @@ -1375,7 +1388,7 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals // Because we only decode the first bitmap in the array, the array header will be ignored. // The bitmap file header of the first image follows the array header. - this.stream.Read(buffer, 0, BmpFileHeader.Size); + stream.Read(buffer, 0, BmpFileHeader.Size); this.fileHeader = BmpFileHeader.Parse(buffer); if (this.fileHeader.Type != BmpConstants.TypeMarkers.Bitmap) { @@ -1398,12 +1411,12 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals /// The color palette. /// Bytes per color palette entry. Usually 4 bytes, but in case of Windows 2.x bitmaps or OS/2 1.x bitmaps /// the bytes per color palette entry's can be 3 bytes instead of 4. + [MemberNotNull(nameof(metadata))] + [MemberNotNull(nameof(bmpMetadata))] private int ReadImageHeaders(BufferedReadStream stream, out bool inverted, out byte[] palette) { - this.stream = stream; - - this.ReadFileHeader(); - this.ReadInfoHeader(); + this.ReadFileHeader(stream); + this.ReadInfoHeader(stream); // see http://www.drdobbs.com/architecture-and-design/the-bmp-file-format-part-1/184409517 // If the height is negative, then this is a Windows bitmap whose origin @@ -1451,13 +1464,13 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals colorMapSizeBytes = this.infoHeader.ClrUsed * bytesPerColorMapEntry; } - palette = null; + palette = Array.Empty(); if (colorMapSizeBytes > 0) { // Usually the color palette is 1024 byte (256 colors * 4), but the documentation does not mention a size limit. // Make sure, that we will not read pass the bitmap offset (starting position of image data). - if ((this.stream.Position + colorMapSizeBytes) > this.fileHeader.Offset) + if ((stream.Position + colorMapSizeBytes) > this.fileHeader.Offset) { BmpThrowHelper.ThrowInvalidImageContentException( $"Reading the color map would read beyond the bitmap offset. Either the color map size of '{colorMapSizeBytes}' is invalid or the bitmap offset."); @@ -1465,21 +1478,21 @@ internal sealed class BmpDecoderCore : IImageDecoderInternals palette = new byte[colorMapSizeBytes]; - if (this.stream.Read(palette, 0, colorMapSizeBytes) == 0) + if (stream.Read(palette, 0, colorMapSizeBytes) == 0) { BmpThrowHelper.ThrowInvalidImageContentException("Could not read enough data for the palette!"); } } - int skipAmount = this.fileHeader.Offset - (int)this.stream.Position; - if ((skipAmount + (int)this.stream.Position) > this.stream.Length) + int skipAmount = this.fileHeader.Offset - (int)stream.Position; + if ((skipAmount + (int)stream.Position) > stream.Length) { BmpThrowHelper.ThrowInvalidImageContentException("Invalid file header offset found. Offset is greater than the stream length."); } if (skipAmount > 0) { - this.stream.Skip(skipAmount); + stream.Skip(skipAmount); } return bytesPerColorMapEntry; diff --git a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs index 9f1e856393..0f432fbda9 100644 --- a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs @@ -1,6 +1,5 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. -#nullable disable using System.Buffers; using System.Buffers.Binary; @@ -72,7 +71,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals /// /// The global configuration. /// - private Configuration configuration; + private Configuration? configuration; /// /// The color depth, in number of bits per pixel. @@ -142,7 +141,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals _ => 0 }; - byte[] iccProfileData = null; + byte[] iccProfileData = Array.Empty(); int iccProfileSize = 0; if (metadata.IccProfile != null) { diff --git a/src/ImageSharp/Processing/Processors/Dithering/PaletteDitherProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Dithering/PaletteDitherProcessor{TPixel}.cs index abf283e6c1..982cc7d46c 100644 --- a/src/ImageSharp/Processing/Processors/Dithering/PaletteDitherProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Dithering/PaletteDitherProcessor{TPixel}.cs @@ -35,7 +35,7 @@ internal sealed class PaletteDitherProcessor : ImageProcessor ReadOnlySpan sourcePalette = definition.Palette.Span; this.paletteOwner = this.Configuration.MemoryAllocator.Allocate(sourcePalette.Length); - Color.ToPixel(this.Configuration, sourcePalette, this.paletteOwner.Memory.Span); + Color.ToPixel(sourcePalette, this.paletteOwner.Memory.Span); this.ditherProcessor = new DitherProcessor( this.Configuration, diff --git a/src/ImageSharp/Processing/Processors/Quantization/IQuantizer.cs b/src/ImageSharp/Processing/Processors/Quantization/IQuantizer.cs index 9d5b606040..f46cea2e0b 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/IQuantizer.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/IQuantizer.cs @@ -21,7 +21,7 @@ public interface IQuantizer /// The to configure internal operations. /// The pixel format. /// The . - IQuantizer CreatePixelSpecificQuantizer(Configuration configuration) + IQuantizer CreatePixelSpecificQuantizer(Configuration? configuration) where TPixel : unmanaged, IPixel; /// @@ -31,6 +31,6 @@ public interface IQuantizer /// The to configure internal operations. /// The options to create the quantizer with. /// The . - IQuantizer CreatePixelSpecificQuantizer(Configuration configuration, QuantizerOptions options) + IQuantizer CreatePixelSpecificQuantizer(Configuration? configuration, QuantizerOptions options) where TPixel : unmanaged, IPixel; } diff --git a/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer.cs b/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer.cs index 0a1032bf0d..0d2450e221 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer.cs @@ -34,12 +34,12 @@ public class OctreeQuantizer : IQuantizer public QuantizerOptions Options { get; } /// - public IQuantizer CreatePixelSpecificQuantizer(Configuration configuration) + public IQuantizer CreatePixelSpecificQuantizer(Configuration? configuration) where TPixel : unmanaged, IPixel => this.CreatePixelSpecificQuantizer(configuration, this.Options); /// - public IQuantizer CreatePixelSpecificQuantizer(Configuration configuration, QuantizerOptions options) + public IQuantizer CreatePixelSpecificQuantizer(Configuration? configuration, QuantizerOptions options) where TPixel : unmanaged, IPixel => new OctreeQuantizer(configuration, options); } diff --git a/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer{TPixel}.cs b/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer{TPixel}.cs index d66a38d80d..37ebc39bac 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer{TPixel}.cs @@ -38,7 +38,7 @@ public struct OctreeQuantizer : IQuantizer /// The configuration which allows altering default behaviour or extending the library. /// The quantizer options defining quantization rules. [MethodImpl(InliningOptions.ShortMethod)] - public OctreeQuantizer(Configuration configuration, QuantizerOptions options) + public OctreeQuantizer(Configuration? configuration, QuantizerOptions options) { Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(options, nameof(options)); diff --git a/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer.cs b/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer.cs index f3ed39c957..c5617fe862 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer.cs @@ -39,19 +39,19 @@ public class PaletteQuantizer : IQuantizer public QuantizerOptions Options { get; } /// - public IQuantizer CreatePixelSpecificQuantizer(Configuration configuration) + public IQuantizer CreatePixelSpecificQuantizer(Configuration? configuration) where TPixel : unmanaged, IPixel => this.CreatePixelSpecificQuantizer(configuration, this.Options); /// - public IQuantizer CreatePixelSpecificQuantizer(Configuration configuration, QuantizerOptions options) + public IQuantizer CreatePixelSpecificQuantizer(Configuration? configuration, QuantizerOptions options) where TPixel : unmanaged, IPixel { Guard.NotNull(options, nameof(options)); // Always use the palette length over options since the palette cannot be reduced. TPixel[] palette = new TPixel[this.colorPalette.Length]; - Color.ToPixel(configuration, this.colorPalette.Span, palette.AsSpan()); + Color.ToPixel(this.colorPalette.Span, palette.AsSpan()); return new PaletteQuantizer(configuration, options, palette); } } diff --git a/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer{TPixel}.cs b/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer{TPixel}.cs index ea7413aab4..182bed2382 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer{TPixel}.cs @@ -29,7 +29,7 @@ internal struct PaletteQuantizer : IQuantizer /// The quantizer options defining quantization rules. /// The palette to use. [MethodImpl(InliningOptions.ShortMethod)] - public PaletteQuantizer(Configuration configuration, QuantizerOptions options, ReadOnlyMemory palette) + public PaletteQuantizer(Configuration? configuration, QuantizerOptions options, ReadOnlyMemory palette) { Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(options, nameof(options)); diff --git a/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer.cs b/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer.cs index 86d798d965..dff9155401 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer.cs @@ -33,12 +33,12 @@ public class WuQuantizer : IQuantizer public QuantizerOptions Options { get; } /// - public IQuantizer CreatePixelSpecificQuantizer(Configuration configuration) + public IQuantizer CreatePixelSpecificQuantizer(Configuration? configuration) where TPixel : unmanaged, IPixel => this.CreatePixelSpecificQuantizer(configuration, this.Options); /// - public IQuantizer CreatePixelSpecificQuantizer(Configuration configuration, QuantizerOptions options) + public IQuantizer CreatePixelSpecificQuantizer(Configuration? configuration, QuantizerOptions options) where TPixel : unmanaged, IPixel => new WuQuantizer(configuration, options); } diff --git a/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer{TPixel}.cs b/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer{TPixel}.cs index 0119558bff..c04dbe1918 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer{TPixel}.cs @@ -85,7 +85,7 @@ internal struct WuQuantizer : IQuantizer /// The configuration which allows altering default behaviour or extending the library. /// The quantizer options defining quantization rules. [MethodImpl(InliningOptions.ShortMethod)] - public WuQuantizer(Configuration configuration, QuantizerOptions options) + public WuQuantizer(Configuration? configuration, QuantizerOptions options) { Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(options, nameof(options)); From cd9a06b4cb6d011b3ebd3e73cfa5944483f6f775 Mon Sep 17 00:00:00 2001 From: Stefan Nikolei Date: Sun, 5 Feb 2023 17:54:39 +0100 Subject: [PATCH 3/9] Fix unit test. --- src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs index 0f432fbda9..e1611b059b 100644 --- a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs @@ -141,7 +141,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals _ => 0 }; - byte[] iccProfileData = Array.Empty(); + byte[]? iccProfileData = null; int iccProfileSize = 0; if (metadata.IccProfile != null) { @@ -181,7 +181,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals /// The metadata. /// The icc profile data. /// The bitmap information header. - private BmpInfoHeader CreateBmpInfoHeader(int width, int height, int infoHeaderSize, short bpp, int bytesPerLine, ImageMetadata metadata, byte[] iccProfileData) + private BmpInfoHeader CreateBmpInfoHeader(int width, int height, int infoHeaderSize, short bpp, int bytesPerLine, ImageMetadata metadata, byte[]? iccProfileData) { int hResolution = 0; int vResolution = 0; @@ -235,7 +235,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals if (this.infoHeaderType is BmpInfoHeaderType.WinVersion5 && metadata.IccProfile != null) { - infoHeader.ProfileSize = iccProfileData.Length; + infoHeader.ProfileSize = iccProfileData!.Length; infoHeader.CsType = BmpColorSpace.PROFILE_EMBEDDED; infoHeader.Intent = BmpRenderingIntent.LCS_GM_IMAGES; } @@ -249,7 +249,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals /// The stream to write to. /// The color profile data. /// The buffer. - private static void WriteColorProfile(Stream stream, byte[] iccProfileData, Span buffer) + private static void WriteColorProfile(Stream stream, byte[]? iccProfileData, Span buffer) { if (iccProfileData != null) { From 7b46791c41f425a1b2a0c24be6d424b53c3943c1 Mon Sep 17 00:00:00 2001 From: Stefan Nikolei Date: Mon, 6 Feb 2023 09:18:44 +0100 Subject: [PATCH 4/9] Remove pragma --- src/ImageSharp/Color/Color.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/ImageSharp/Color/Color.cs b/src/ImageSharp/Color/Color.cs index 590a3ed311..13af25f6c7 100644 --- a/src/ImageSharp/Color/Color.cs +++ b/src/ImageSharp/Color/Color.cs @@ -289,9 +289,7 @@ public readonly partial struct Color : IEquatable /// The source color span. /// The destination pixel span. [MethodImpl(InliningOptions.ShortMethod)] -#pragma warning disable RCS1163 // Unused parameter. public static void ToPixel(ReadOnlySpan source, Span destination) -#pragma warning restore RCS1163 // Unused parameter. where TPixel : unmanaged, IPixel { // TODO: Investigate bulk operations utilizing configuration parameter here. From cc4647741ae58a17280ef35d51c5e22c056dd156 Mon Sep 17 00:00:00 2001 From: Stefan Nikolei Date: Mon, 6 Feb 2023 10:43:26 +0100 Subject: [PATCH 5/9] Pass configuration as parameter --- src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs | 79 ++++++++++--------- .../Processors/Quantization/IQuantizer.cs | 4 +- .../Quantization/OctreeQuantizer.cs | 4 +- .../Quantization/PaletteQuantizer.cs | 4 +- .../Processors/Quantization/WuQuantizer.cs | 4 +- 5 files changed, 50 insertions(+), 45 deletions(-) diff --git a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs index e1611b059b..eaf6b4df7f 100644 --- a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs @@ -68,11 +68,6 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals /// private readonly MemoryAllocator memoryAllocator; - /// - /// The global configuration. - /// - private Configuration? configuration; - /// /// The color depth, in number of bits per pixel. /// @@ -123,7 +118,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals Guard.NotNull(image, nameof(image)); Guard.NotNull(stream, nameof(stream)); - this.configuration = image.GetConfiguration(); + Configuration configuration = image.GetConfiguration(); ImageMetadata metadata = image.Metadata; BmpMetadata bmpMetadata = metadata.GetBmpMetadata(); this.bitsPerPixel ??= bmpMetadata.BitsPerPixel; @@ -164,7 +159,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals WriteBitmapFileHeader(stream, infoHeaderSize, colorPaletteSize, iccProfileSize, infoHeader, buffer); this.WriteBitmapInfoHeader(stream, infoHeader, buffer, infoHeaderSize); - this.WriteImage(stream, image); + this.WriteImage(stream, image, configuration); WriteColorProfile(stream, iccProfileData, buffer); stream.Flush(); @@ -316,38 +311,39 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals /// /// The containing pixel data. /// - private void WriteImage(Stream stream, Image image) + /// The global configuration. + private void WriteImage(Stream stream, Image image, Configuration configuration) where TPixel : unmanaged, IPixel { Buffer2D pixels = image.Frames.RootFrame.PixelBuffer; switch (this.bitsPerPixel) { case BmpBitsPerPixel.Pixel32: - this.Write32BitPixelData(stream, pixels); + this.Write32BitPixelData(stream, pixels, configuration); break; case BmpBitsPerPixel.Pixel24: - this.Write24BitPixelData(stream, pixels); + this.Write24BitPixelData(stream, pixels, configuration); break; case BmpBitsPerPixel.Pixel16: - this.Write16BitPixelData(stream, pixels); + this.Write16BitPixelData(stream, pixels, configuration); break; case BmpBitsPerPixel.Pixel8: - this.Write8BitPixelData(stream, image); + this.Write8BitPixelData(stream, image, configuration); break; case BmpBitsPerPixel.Pixel4: - this.Write4BitPixelData(stream, image); + this.Write4BitPixelData(stream, image, configuration); break; case BmpBitsPerPixel.Pixel2: - this.Write2BitPixelData(stream, image); + this.Write2BitPixelData(stream, image, configuration); break; case BmpBitsPerPixel.Pixel1: - this.Write1BitPixelData(stream, image); + this.Write1BitPixelData(stream, image, configuration); break; } } @@ -361,7 +357,8 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals /// The pixel format. /// The to write to. /// The containing pixel data. - private void Write32BitPixelData(Stream stream, Buffer2D pixels) + /// The global configuration. + private void Write32BitPixelData(Stream stream, Buffer2D pixels, Configuration configuration) where TPixel : unmanaged, IPixel { using IMemoryOwner row = this.AllocateRow(pixels.Width, 4); @@ -371,7 +368,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals { Span pixelSpan = pixels.DangerousGetRowSpan(y); PixelOperations.Instance.ToBgra32Bytes( - this.configuration, + configuration, pixelSpan, rowSpan, pixelSpan.Length); @@ -385,7 +382,8 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals /// The pixel format. /// The to write to. /// The containing pixel data. - private void Write24BitPixelData(Stream stream, Buffer2D pixels) + /// The global configuration. + private void Write24BitPixelData(Stream stream, Buffer2D pixels, Configuration configuration) where TPixel : unmanaged, IPixel { int width = pixels.Width; @@ -397,7 +395,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals { Span pixelSpan = pixels.DangerousGetRowSpan(y); PixelOperations.Instance.ToBgr24Bytes( - this.configuration, + configuration, pixelSpan, row.Slice(0, rowBytesWithoutPadding), width); @@ -411,7 +409,8 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals /// The type of the pixel. /// The to write to. /// The containing pixel data. - private void Write16BitPixelData(Stream stream, Buffer2D pixels) + /// The global configuration. + private void Write16BitPixelData(Stream stream, Buffer2D pixels, Configuration configuration) where TPixel : unmanaged, IPixel { int width = pixels.Width; @@ -424,7 +423,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals Span pixelSpan = pixels.DangerousGetRowSpan(y); PixelOperations.Instance.ToBgra5551Bytes( - this.configuration, + configuration, pixelSpan, row.Slice(0, rowBytesWithoutPadding), pixelSpan.Length); @@ -439,7 +438,8 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals /// The type of the pixel. /// The to write to. /// The containing pixel data. - private void Write8BitPixelData(Stream stream, Image image) + /// The global configuration. + private void Write8BitPixelData(Stream stream, Image image, Configuration configuration) where TPixel : unmanaged, IPixel { bool isL8 = typeof(TPixel) == typeof(L8); @@ -452,7 +452,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals } else { - this.Write8BitColor(stream, image, colorPalette); + this.Write8BitColor(stream, image, colorPalette, configuration); } } @@ -463,16 +463,17 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals /// The to write to. /// The containing pixel data. /// A byte span of size 1024 for the color palette. - private void Write8BitColor(Stream stream, Image image, Span colorPalette) + /// The global configuration + private void Write8BitColor(Stream stream, Image image, Span colorPalette, Configuration configuration) where TPixel : unmanaged, IPixel { - using IQuantizer frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer(this.configuration); + using IQuantizer frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer(configuration); frameQuantizer.BuildPalette(this.pixelSamplingStrategy, image); using IndexedImageFrame quantized = frameQuantizer.QuantizeFrame(image.Frames.RootFrame, image.Bounds); ReadOnlySpan quantizedColorPalette = quantized.Palette.Span; - this.WriteColorPalette(stream, quantizedColorPalette, colorPalette); + this.WriteColorPalette(stream, quantizedColorPalette, colorPalette, configuration); for (int y = image.Height - 1; y >= 0; y--) { @@ -530,10 +531,11 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals /// The type of the pixel. /// The to write to. /// The containing pixel data. - private void Write4BitPixelData(Stream stream, Image image) + /// The global configuration. + private void Write4BitPixelData(Stream stream, Image image, Configuration configuration) where TPixel : unmanaged, IPixel { - using IQuantizer frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer(this.configuration, new QuantizerOptions() + using IQuantizer frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer(configuration, new QuantizerOptions() { MaxColors = 16 }); @@ -545,7 +547,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals Span colorPalette = colorPaletteBuffer.GetSpan(); ReadOnlySpan quantizedColorPalette = quantized.Palette.Span; - this.WriteColorPalette(stream, quantizedColorPalette, colorPalette); + this.WriteColorPalette(stream, quantizedColorPalette, colorPalette, configuration); ReadOnlySpan pixelRowSpan = quantized.DangerousGetRowSpan(0); int rowPadding = pixelRowSpan.Length % 2 != 0 ? this.padding - 1 : this.padding; @@ -577,10 +579,11 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals /// The type of the pixel. /// The to write to. /// The containing pixel data. - private void Write2BitPixelData(Stream stream, Image image) + /// The global configuration + private void Write2BitPixelData(Stream stream, Image image, Configuration configuration) where TPixel : unmanaged, IPixel { - using IQuantizer frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer(this.configuration, new QuantizerOptions() + using IQuantizer frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer(configuration, new QuantizerOptions() { MaxColors = 4 }); @@ -592,7 +595,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals Span colorPalette = colorPaletteBuffer.GetSpan(); ReadOnlySpan quantizedColorPalette = quantized.Palette.Span; - this.WriteColorPalette(stream, quantizedColorPalette, colorPalette); + this.WriteColorPalette(stream, quantizedColorPalette, colorPalette, configuration); ReadOnlySpan pixelRowSpan = quantized.DangerousGetRowSpan(0); int rowPadding = pixelRowSpan.Length % 4 != 0 ? this.padding - 1 : this.padding; @@ -633,10 +636,11 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals /// The type of the pixel. /// The to write to. /// The containing pixel data. - private void Write1BitPixelData(Stream stream, Image image) + /// The global configuration + private void Write1BitPixelData(Stream stream, Image image, Configuration configuration) where TPixel : unmanaged, IPixel { - using IQuantizer frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer(this.configuration, new QuantizerOptions() + using IQuantizer frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer(configuration, new QuantizerOptions() { MaxColors = 2 }); @@ -648,7 +652,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals Span colorPalette = colorPaletteBuffer.GetSpan(); ReadOnlySpan quantizedColorPalette = quantized.Palette.Span; - this.WriteColorPalette(stream, quantizedColorPalette, colorPalette); + this.WriteColorPalette(stream, quantizedColorPalette, colorPalette, configuration); ReadOnlySpan quantizedPixelRow = quantized.DangerousGetRowSpan(0); int rowPadding = quantizedPixelRow.Length % 8 != 0 ? this.padding - 1 : this.padding; @@ -683,11 +687,12 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals /// The to write to. /// The color palette from the quantized image. /// A temporary byte span to write the color palette to. - private void WriteColorPalette(Stream stream, ReadOnlySpan quantizedColorPalette, Span colorPalette) + /// The global configuration + private void WriteColorPalette(Stream stream, ReadOnlySpan quantizedColorPalette, Span colorPalette, Configuration configuration) where TPixel : unmanaged, IPixel { int quantizedColorBytes = quantizedColorPalette.Length * 4; - PixelOperations.Instance.ToBgra32(this.configuration, quantizedColorPalette, MemoryMarshal.Cast(colorPalette[..quantizedColorBytes])); + PixelOperations.Instance.ToBgra32(configuration, quantizedColorPalette, MemoryMarshal.Cast(colorPalette[..quantizedColorBytes])); Span colorPaletteAsUInt = MemoryMarshal.Cast(colorPalette); for (int i = 0; i < colorPaletteAsUInt.Length; i++) { diff --git a/src/ImageSharp/Processing/Processors/Quantization/IQuantizer.cs b/src/ImageSharp/Processing/Processors/Quantization/IQuantizer.cs index f46cea2e0b..9d5b606040 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/IQuantizer.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/IQuantizer.cs @@ -21,7 +21,7 @@ public interface IQuantizer /// The to configure internal operations. /// The pixel format. /// The . - IQuantizer CreatePixelSpecificQuantizer(Configuration? configuration) + IQuantizer CreatePixelSpecificQuantizer(Configuration configuration) where TPixel : unmanaged, IPixel; /// @@ -31,6 +31,6 @@ public interface IQuantizer /// The to configure internal operations. /// The options to create the quantizer with. /// The . - IQuantizer CreatePixelSpecificQuantizer(Configuration? configuration, QuantizerOptions options) + IQuantizer CreatePixelSpecificQuantizer(Configuration configuration, QuantizerOptions options) where TPixel : unmanaged, IPixel; } diff --git a/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer.cs b/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer.cs index 0d2450e221..0a1032bf0d 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer.cs @@ -34,12 +34,12 @@ public class OctreeQuantizer : IQuantizer public QuantizerOptions Options { get; } /// - public IQuantizer CreatePixelSpecificQuantizer(Configuration? configuration) + public IQuantizer CreatePixelSpecificQuantizer(Configuration configuration) where TPixel : unmanaged, IPixel => this.CreatePixelSpecificQuantizer(configuration, this.Options); /// - public IQuantizer CreatePixelSpecificQuantizer(Configuration? configuration, QuantizerOptions options) + public IQuantizer CreatePixelSpecificQuantizer(Configuration configuration, QuantizerOptions options) where TPixel : unmanaged, IPixel => new OctreeQuantizer(configuration, options); } diff --git a/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer.cs b/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer.cs index c5617fe862..fe4af9005a 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer.cs @@ -39,12 +39,12 @@ public class PaletteQuantizer : IQuantizer public QuantizerOptions Options { get; } /// - public IQuantizer CreatePixelSpecificQuantizer(Configuration? configuration) + public IQuantizer CreatePixelSpecificQuantizer(Configuration configuration) where TPixel : unmanaged, IPixel => this.CreatePixelSpecificQuantizer(configuration, this.Options); /// - public IQuantizer CreatePixelSpecificQuantizer(Configuration? configuration, QuantizerOptions options) + public IQuantizer CreatePixelSpecificQuantizer(Configuration configuration, QuantizerOptions options) where TPixel : unmanaged, IPixel { Guard.NotNull(options, nameof(options)); diff --git a/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer.cs b/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer.cs index dff9155401..86d798d965 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer.cs @@ -33,12 +33,12 @@ public class WuQuantizer : IQuantizer public QuantizerOptions Options { get; } /// - public IQuantizer CreatePixelSpecificQuantizer(Configuration? configuration) + public IQuantizer CreatePixelSpecificQuantizer(Configuration configuration) where TPixel : unmanaged, IPixel => this.CreatePixelSpecificQuantizer(configuration, this.Options); /// - public IQuantizer CreatePixelSpecificQuantizer(Configuration? configuration, QuantizerOptions options) + public IQuantizer CreatePixelSpecificQuantizer(Configuration configuration, QuantizerOptions options) where TPixel : unmanaged, IPixel => new WuQuantizer(configuration, options); } From 4a82d27303519c1da92f6432fcaef7bd8ef96603 Mon Sep 17 00:00:00 2001 From: Stefan Nikolei Date: Mon, 6 Feb 2023 10:50:12 +0100 Subject: [PATCH 6/9] Check iccProfileData for null not the profile --- src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs index eaf6b4df7f..1479d31e64 100644 --- a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs @@ -228,9 +228,9 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals infoHeader.Compression = BmpCompression.BitFields; } - if (this.infoHeaderType is BmpInfoHeaderType.WinVersion5 && metadata.IccProfile != null) + if (this.infoHeaderType is BmpInfoHeaderType.WinVersion5 && iccProfileData != null) { - infoHeader.ProfileSize = iccProfileData!.Length; + infoHeader.ProfileSize = iccProfileData.Length; infoHeader.CsType = BmpColorSpace.PROFILE_EMBEDDED; infoHeader.Intent = BmpRenderingIntent.LCS_GM_IMAGES; } From 1c773661b4c19881cf844ebf19cf2ab3c93c1b1e Mon Sep 17 00:00:00 2001 From: Stefan Nikolei Date: Mon, 6 Feb 2023 10:54:56 +0100 Subject: [PATCH 7/9] Make method static --- src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs index 1479d31e64..620e0499f4 100644 --- a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs @@ -473,7 +473,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals using IndexedImageFrame quantized = frameQuantizer.QuantizeFrame(image.Frames.RootFrame, image.Bounds); ReadOnlySpan quantizedColorPalette = quantized.Palette.Span; - this.WriteColorPalette(stream, quantizedColorPalette, colorPalette, configuration); + WriteColorPalette(stream, quantizedColorPalette, colorPalette, configuration); for (int y = image.Height - 1; y >= 0; y--) { @@ -547,7 +547,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals Span colorPalette = colorPaletteBuffer.GetSpan(); ReadOnlySpan quantizedColorPalette = quantized.Palette.Span; - this.WriteColorPalette(stream, quantizedColorPalette, colorPalette, configuration); + WriteColorPalette(stream, quantizedColorPalette, colorPalette, configuration); ReadOnlySpan pixelRowSpan = quantized.DangerousGetRowSpan(0); int rowPadding = pixelRowSpan.Length % 2 != 0 ? this.padding - 1 : this.padding; @@ -595,7 +595,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals Span colorPalette = colorPaletteBuffer.GetSpan(); ReadOnlySpan quantizedColorPalette = quantized.Palette.Span; - this.WriteColorPalette(stream, quantizedColorPalette, colorPalette, configuration); + WriteColorPalette(stream, quantizedColorPalette, colorPalette, configuration); ReadOnlySpan pixelRowSpan = quantized.DangerousGetRowSpan(0); int rowPadding = pixelRowSpan.Length % 4 != 0 ? this.padding - 1 : this.padding; @@ -652,7 +652,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals Span colorPalette = colorPaletteBuffer.GetSpan(); ReadOnlySpan quantizedColorPalette = quantized.Palette.Span; - this.WriteColorPalette(stream, quantizedColorPalette, colorPalette, configuration); + WriteColorPalette(stream, quantizedColorPalette, colorPalette, configuration); ReadOnlySpan quantizedPixelRow = quantized.DangerousGetRowSpan(0); int rowPadding = quantizedPixelRow.Length % 8 != 0 ? this.padding - 1 : this.padding; @@ -688,7 +688,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals /// The color palette from the quantized image. /// A temporary byte span to write the color palette to. /// The global configuration - private void WriteColorPalette(Stream stream, ReadOnlySpan quantizedColorPalette, Span colorPalette, Configuration configuration) + private static void WriteColorPalette(Stream stream, ReadOnlySpan quantizedColorPalette, Span colorPalette, Configuration configuration) where TPixel : unmanaged, IPixel { int quantizedColorBytes = quantizedColorPalette.Length * 4; From 88ddf709c57bf72d40ee04777ebb9f15e003d5c9 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Mon, 6 Feb 2023 20:02:11 +0100 Subject: [PATCH 8/9] Actually call arm methods, if arm is supported --- src/ImageSharp/Compression/Zlib/Adler32.cs | 5 +++++ src/ImageSharp/Compression/Zlib/Crc32.cs | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/ImageSharp/Compression/Zlib/Adler32.cs b/src/ImageSharp/Compression/Zlib/Adler32.cs index 1b1a77715d..dd8217541a 100644 --- a/src/ImageSharp/Compression/Zlib/Adler32.cs +++ b/src/ImageSharp/Compression/Zlib/Adler32.cs @@ -71,6 +71,11 @@ internal static class Adler32 return CalculateSse(adler, buffer); } + if (AdvSimd.IsSupported) + { + return CalculateArm(adler, buffer); + } + return CalculateScalar(adler, buffer); } diff --git a/src/ImageSharp/Compression/Zlib/Crc32.cs b/src/ImageSharp/Compression/Zlib/Crc32.cs index c85d58df5b..39c535c773 100644 --- a/src/ImageSharp/Compression/Zlib/Crc32.cs +++ b/src/ImageSharp/Compression/Zlib/Crc32.cs @@ -61,6 +61,16 @@ internal static partial class Crc32 return ~CalculateSse(~crc, buffer); } + if (ArmCrc32.Arm64.IsSupported) + { + return ~CalculateArm64(~crc, buffer); + } + + if (ArmCrc32.IsSupported) + { + return ~CalculateArm(~crc, buffer); + } + return ~CalculateScalar(~crc, buffer); } From 66d494e2505454320ab6052966eb91366f8ca09d Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Tue, 7 Feb 2023 09:17:16 +1000 Subject: [PATCH 9/9] Reorder params, fix other warnings. --- src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs | 68 +++++++++---------- .../Quantization/OctreeQuantizer{TPixel}.cs | 7 +- .../Quantization/PaletteQuantizer{TPixel}.cs | 11 ++- .../Quantization/WuQuantizer{TPixel}.cs | 8 +-- 4 files changed, 44 insertions(+), 50 deletions(-) diff --git a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs index 620e0499f4..f30369d19b 100644 --- a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs @@ -159,7 +159,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals WriteBitmapFileHeader(stream, infoHeaderSize, colorPaletteSize, iccProfileSize, infoHeader, buffer); this.WriteBitmapInfoHeader(stream, infoHeader, buffer, infoHeaderSize); - this.WriteImage(stream, image, configuration); + this.WriteImage(configuration, stream, image); WriteColorProfile(stream, iccProfileData, buffer); stream.Flush(); @@ -307,43 +307,43 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals /// Writes the pixel data to the binary stream. /// /// The pixel format. + /// The global configuration. /// The to write to. /// /// The containing pixel data. /// - /// The global configuration. - private void WriteImage(Stream stream, Image image, Configuration configuration) + private void WriteImage(Configuration configuration, Stream stream, Image image) where TPixel : unmanaged, IPixel { Buffer2D pixels = image.Frames.RootFrame.PixelBuffer; switch (this.bitsPerPixel) { case BmpBitsPerPixel.Pixel32: - this.Write32BitPixelData(stream, pixels, configuration); + this.Write32BitPixelData(configuration, stream, pixels); break; case BmpBitsPerPixel.Pixel24: - this.Write24BitPixelData(stream, pixels, configuration); + this.Write24BitPixelData(configuration, stream, pixels); break; case BmpBitsPerPixel.Pixel16: - this.Write16BitPixelData(stream, pixels, configuration); + this.Write16BitPixelData(configuration, stream, pixels); break; case BmpBitsPerPixel.Pixel8: - this.Write8BitPixelData(stream, image, configuration); + this.Write8BitPixelData(configuration, stream, image); break; case BmpBitsPerPixel.Pixel4: - this.Write4BitPixelData(stream, image, configuration); + this.Write4BitPixelData(configuration, stream, image); break; case BmpBitsPerPixel.Pixel2: - this.Write2BitPixelData(stream, image, configuration); + this.Write2BitPixelData(configuration, stream, image); break; case BmpBitsPerPixel.Pixel1: - this.Write1BitPixelData(stream, image, configuration); + this.Write1BitPixelData(configuration, stream, image); break; } } @@ -355,10 +355,10 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals /// Writes 32-bit data with a color palette to the stream. /// /// The pixel format. + /// The global configuration. /// The to write to. /// The containing pixel data. - /// The global configuration. - private void Write32BitPixelData(Stream stream, Buffer2D pixels, Configuration configuration) + private void Write32BitPixelData(Configuration configuration, Stream stream, Buffer2D pixels) where TPixel : unmanaged, IPixel { using IMemoryOwner row = this.AllocateRow(pixels.Width, 4); @@ -380,10 +380,10 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals /// Writes 24-bit pixel data with a color palette to the stream. /// /// The pixel format. + /// The global configuration. /// The to write to. /// The containing pixel data. - /// The global configuration. - private void Write24BitPixelData(Stream stream, Buffer2D pixels, Configuration configuration) + private void Write24BitPixelData(Configuration configuration, Stream stream, Buffer2D pixels) where TPixel : unmanaged, IPixel { int width = pixels.Width; @@ -407,11 +407,11 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals /// Writes 16-bit pixel data with a color palette to the stream. /// /// The type of the pixel. + /// The global configuration. /// The to write to. /// The containing pixel data. - /// The global configuration. - private void Write16BitPixelData(Stream stream, Buffer2D pixels, Configuration configuration) - where TPixel : unmanaged, IPixel + private void Write16BitPixelData(Configuration configuration, Stream stream, Buffer2D pixels) + where TPixel : unmanaged, IPixel { int width = pixels.Width; int rowBytesWithoutPadding = width * 2; @@ -436,10 +436,10 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals /// Writes 8 bit pixel data with a color palette. The color palette has 256 entry's with 4 bytes for each entry. /// /// The type of the pixel. + /// The global configuration. /// The to write to. /// The containing pixel data. - /// The global configuration. - private void Write8BitPixelData(Stream stream, Image image, Configuration configuration) + private void Write8BitPixelData(Configuration configuration, Stream stream, Image image) where TPixel : unmanaged, IPixel { bool isL8 = typeof(TPixel) == typeof(L8); @@ -452,7 +452,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals } else { - this.Write8BitColor(stream, image, colorPalette, configuration); + this.Write8BitColor(configuration, stream, image, colorPalette); } } @@ -460,11 +460,11 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals /// Writes an 8 bit color image with a color palette. The color palette has 256 entry's with 4 bytes for each entry. /// /// The type of the pixel. + /// The global configuration. /// The to write to. /// The containing pixel data. /// A byte span of size 1024 for the color palette. - /// The global configuration - private void Write8BitColor(Stream stream, Image image, Span colorPalette, Configuration configuration) + private void Write8BitColor(Configuration configuration, Stream stream, Image image, Span colorPalette) where TPixel : unmanaged, IPixel { using IQuantizer frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer(configuration); @@ -473,7 +473,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals using IndexedImageFrame quantized = frameQuantizer.QuantizeFrame(image.Frames.RootFrame, image.Bounds); ReadOnlySpan quantizedColorPalette = quantized.Palette.Span; - WriteColorPalette(stream, quantizedColorPalette, colorPalette, configuration); + WriteColorPalette(configuration, stream, quantizedColorPalette, colorPalette); for (int y = image.Height - 1; y >= 0; y--) { @@ -529,10 +529,10 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals /// Writes 4 bit pixel data with a color palette. The color palette has 16 entry's with 4 bytes for each entry. /// /// The type of the pixel. + /// The global configuration. /// The to write to. /// The containing pixel data. - /// The global configuration. - private void Write4BitPixelData(Stream stream, Image image, Configuration configuration) + private void Write4BitPixelData(Configuration configuration, Stream stream, Image image) where TPixel : unmanaged, IPixel { using IQuantizer frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer(configuration, new QuantizerOptions() @@ -547,7 +547,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals Span colorPalette = colorPaletteBuffer.GetSpan(); ReadOnlySpan quantizedColorPalette = quantized.Palette.Span; - WriteColorPalette(stream, quantizedColorPalette, colorPalette, configuration); + WriteColorPalette(configuration, stream, quantizedColorPalette, colorPalette); ReadOnlySpan pixelRowSpan = quantized.DangerousGetRowSpan(0); int rowPadding = pixelRowSpan.Length % 2 != 0 ? this.padding - 1 : this.padding; @@ -577,10 +577,10 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals /// Writes 2 bit pixel data with a color palette. The color palette has 4 entry's with 4 bytes for each entry. /// /// The type of the pixel. + /// The global configuration. /// The to write to. /// The containing pixel data. - /// The global configuration - private void Write2BitPixelData(Stream stream, Image image, Configuration configuration) + private void Write2BitPixelData(Configuration configuration, Stream stream, Image image) where TPixel : unmanaged, IPixel { using IQuantizer frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer(configuration, new QuantizerOptions() @@ -595,7 +595,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals Span colorPalette = colorPaletteBuffer.GetSpan(); ReadOnlySpan quantizedColorPalette = quantized.Palette.Span; - WriteColorPalette(stream, quantizedColorPalette, colorPalette, configuration); + WriteColorPalette(configuration, stream, quantizedColorPalette, colorPalette); ReadOnlySpan pixelRowSpan = quantized.DangerousGetRowSpan(0); int rowPadding = pixelRowSpan.Length % 4 != 0 ? this.padding - 1 : this.padding; @@ -634,10 +634,10 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals /// Writes 1 bit pixel data with a color palette. The color palette has 2 entry's with 4 bytes for each entry. /// /// The type of the pixel. + /// The global configuration. /// The to write to. /// The containing pixel data. - /// The global configuration - private void Write1BitPixelData(Stream stream, Image image, Configuration configuration) + private void Write1BitPixelData(Configuration configuration, Stream stream, Image image) where TPixel : unmanaged, IPixel { using IQuantizer frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer(configuration, new QuantizerOptions() @@ -652,7 +652,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals Span colorPalette = colorPaletteBuffer.GetSpan(); ReadOnlySpan quantizedColorPalette = quantized.Palette.Span; - WriteColorPalette(stream, quantizedColorPalette, colorPalette, configuration); + WriteColorPalette(configuration, stream, quantizedColorPalette, colorPalette); ReadOnlySpan quantizedPixelRow = quantized.DangerousGetRowSpan(0); int rowPadding = quantizedPixelRow.Length % 8 != 0 ? this.padding - 1 : this.padding; @@ -684,11 +684,11 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals /// Writes the color palette to the stream. The color palette has 4 bytes for each entry. /// /// The type of the pixel. + /// The global configuration. /// The to write to. /// The color palette from the quantized image. /// A temporary byte span to write the color palette to. - /// The global configuration - private static void WriteColorPalette(Stream stream, ReadOnlySpan quantizedColorPalette, Span colorPalette, Configuration configuration) + private static void WriteColorPalette(Configuration configuration, Stream stream, ReadOnlySpan quantizedColorPalette, Span colorPalette) where TPixel : unmanaged, IPixel { int quantizedColorBytes = quantizedColorPalette.Length * 4; diff --git a/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer{TPixel}.cs b/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer{TPixel}.cs index 37ebc39bac..1136fbc9da 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer{TPixel}.cs @@ -26,7 +26,7 @@ public struct OctreeQuantizer : IQuantizer private readonly int maxColors; private readonly int bitDepth; private readonly Octree octree; - private IMemoryOwner paletteOwner; + private readonly IMemoryOwner paletteOwner; private ReadOnlyMemory palette; private EuclideanPixelMap? pixelMap; private readonly bool isDithering; @@ -38,11 +38,8 @@ public struct OctreeQuantizer : IQuantizer /// The configuration which allows altering default behaviour or extending the library. /// The quantizer options defining quantization rules. [MethodImpl(InliningOptions.ShortMethod)] - public OctreeQuantizer(Configuration? configuration, QuantizerOptions options) + public OctreeQuantizer(Configuration configuration, QuantizerOptions options) { - Guard.NotNull(configuration, nameof(configuration)); - Guard.NotNull(options, nameof(options)); - this.Configuration = configuration; this.Options = options; diff --git a/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer{TPixel}.cs b/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer{TPixel}.cs index 182bed2382..86db9f6f01 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer{TPixel}.cs @@ -17,10 +17,10 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization; "Design", "CA1001:Types that own disposable fields should be disposable", Justification = "https://github.com/dotnet/roslyn-analyzers/issues/6151")] -internal struct PaletteQuantizer : IQuantizer +internal readonly struct PaletteQuantizer : IQuantizer where TPixel : unmanaged, IPixel { - private EuclideanPixelMap pixelMap; + private readonly EuclideanPixelMap pixelMap; /// /// Initializes a new instance of the struct. @@ -29,7 +29,7 @@ internal struct PaletteQuantizer : IQuantizer /// The quantizer options defining quantization rules. /// The palette to use. [MethodImpl(InliningOptions.ShortMethod)] - public PaletteQuantizer(Configuration? configuration, QuantizerOptions options, ReadOnlyMemory palette) + public PaletteQuantizer(Configuration configuration, QuantizerOptions options, ReadOnlyMemory palette) { Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(options, nameof(options)); @@ -65,8 +65,5 @@ internal struct PaletteQuantizer : IQuantizer => (byte)this.pixelMap.GetClosestColor(color, out match); /// - public void Dispose() - { - this.pixelMap.Dispose(); - } + public void Dispose() => this.pixelMap.Dispose(); } diff --git a/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer{TPixel}.cs b/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer{TPixel}.cs index c04dbe1918..edf293d39e 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer{TPixel}.cs @@ -69,9 +69,9 @@ internal struct WuQuantizer : IQuantizer /// private const int TableLength = IndexCount * IndexCount * IndexCount * IndexAlphaCount; - private IMemoryOwner momentsOwner; - private IMemoryOwner tagsOwner; - private IMemoryOwner paletteOwner; + private readonly IMemoryOwner momentsOwner; + private readonly IMemoryOwner tagsOwner; + private readonly IMemoryOwner paletteOwner; private ReadOnlyMemory palette; private int maxColors; private readonly Box[] colorCube; @@ -85,7 +85,7 @@ internal struct WuQuantizer : IQuantizer /// The configuration which allows altering default behaviour or extending the library. /// The quantizer options defining quantization rules. [MethodImpl(InliningOptions.ShortMethod)] - public WuQuantizer(Configuration? configuration, QuantizerOptions options) + public WuQuantizer(Configuration configuration, QuantizerOptions options) { Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(options, nameof(options));