From 787ffa57eeee862755d039c0ca672f8b1ef86aac Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Mon, 23 Aug 2021 17:04:57 +0300 Subject: [PATCH] Removed unused methods & constructor, fixed warnings --- .../Formats/Jpeg/Components/Block8x8.cs | 77 ++++++------------- .../Formats/Jpg/Block8x8FTests.cs | 4 +- .../Formats/Jpg/Block8x8Tests.cs | 38 +++------ .../Jpg/Utils/LibJpegTools.ComponentData.cs | 2 +- 4 files changed, 34 insertions(+), 87 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Block8x8.cs b/src/ImageSharp/Formats/Jpeg/Components/Block8x8.cs index bc6036903..d61a3c6fd 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Block8x8.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Block8x8.cs @@ -28,17 +28,6 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components /// private fixed short data[Size]; - /// - /// Initializes a new instance of the struct. - /// - /// A of coefficients - public Block8x8(Span coefficients) - { - ref byte selfRef = ref Unsafe.As(ref this); - ref byte sourceRef = ref Unsafe.As(ref MemoryMarshal.GetReference(coefficients)); - Unsafe.CopyBlock(ref selfRef, ref sourceRef, Size * sizeof(short)); - } - /// /// Gets or sets a value at the given index /// @@ -75,15 +64,9 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components set => this[(y * 8) + x] = value; } - public static bool operator ==(Block8x8 left, Block8x8 right) - { - return left.Equals(right); - } + public static bool operator ==(Block8x8 left, Block8x8 right) => left.Equals(right); - public static bool operator !=(Block8x8 left, Block8x8 right) - { - return !left.Equals(right); - } + public static bool operator !=(Block8x8 left, Block8x8 right) => !left.Equals(right); /// /// Multiply all elements by a given @@ -149,34 +132,11 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components return result; } - /// - /// Pointer-based "Indexer" (getter part) - /// - /// Block pointer - /// Index - /// The scaleVec value at the specified index - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static short GetScalarAt(Block8x8* blockPtr, int idx) - { - GuardBlockIndex(idx); - - short* fp = blockPtr->data; - return fp[idx]; - } - - /// - /// Pointer-based "Indexer" (setter part) - /// - /// Block pointer - /// Index - /// Value - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void SetScalarAt(Block8x8* blockPtr, int idx, short value) + public static Block8x8 Load(Span data) { - GuardBlockIndex(idx); - - short* fp = blockPtr->data; - fp[idx] = value; + Block8x8 result = default; + result.LoadFrom(data); + return result; } /// @@ -194,7 +154,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components /// public short[] ToArray() { - var result = new short[Size]; + short[] result = new short[Size]; this.CopyTo(result); return result; } @@ -220,6 +180,19 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components } } + /// + /// Load raw 16bit integers from source. + /// + /// Source + [MethodImpl(InliningOptions.ShortMethod)] + public void LoadFrom(Span source) + { + ref byte s = ref Unsafe.As(ref MemoryMarshal.GetReference(source)); + ref byte d = ref Unsafe.As(ref this); + + Unsafe.CopyBlock(ref d, ref s, Size * sizeof(short)); + } + /// /// Cast and copy -s from the beginning of 'source' span. /// @@ -271,16 +244,10 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components } /// - public override bool Equals(object obj) - { - return obj is Block8x8 other && this.Equals(other); - } + public override bool Equals(object obj) => obj is Block8x8 other && this.Equals(other); /// - public override int GetHashCode() - { - return (this[0] * 31) + this[1]; - } + public override int GetHashCode() => (this[0] * 31) + this[1]; /// /// Calculate the total sum of absolute differences of elements in 'a' and 'b'. diff --git a/tests/ImageSharp.Tests/Formats/Jpg/Block8x8FTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/Block8x8FTests.cs index c68b0ffa8..42fdd603e 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/Block8x8FTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/Block8x8FTests.cs @@ -462,7 +462,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg short[] data = Create8x8ShortData(); - var source = new Block8x8(data); + var source = Block8x8.Load(data); Block8x8F dest = default; dest.LoadFromInt16Scalar(ref source); @@ -483,7 +483,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg short[] data = Create8x8ShortData(); - var source = new Block8x8(data); + var source = Block8x8.Load(data); Block8x8F dest = default; dest.LoadFromInt16ExtendedAvx2(ref source); diff --git a/tests/ImageSharp.Tests/Formats/Jpg/Block8x8Tests.cs b/tests/ImageSharp.Tests/Formats/Jpg/Block8x8Tests.cs index 9195f0915..afe71ad04 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/Block8x8Tests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/Block8x8Tests.cs @@ -22,7 +22,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg { short[] data = Create8x8ShortData(); - var block = new Block8x8(data); + var block = Block8x8.Load(data); for (int i = 0; i < Block8x8.Size; i++) { @@ -43,32 +43,12 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg Assert.Equal(42, block[42]); } - [Fact] - public unsafe void Indexer_GetScalarAt_SetScalarAt() - { - int sum; - var block = default(Block8x8); - - for (int i = 0; i < Block8x8.Size; i++) - { - Block8x8.SetScalarAt(&block, i, (short)i); - } - - sum = 0; - for (int i = 0; i < Block8x8.Size; i++) - { - sum += Block8x8.GetScalarAt(&block, i); - } - - Assert.Equal(sum, 64 * 63 / 2); - } - [Fact] public void AsFloatBlock() { short[] data = Create8x8ShortData(); - var source = new Block8x8(data); + var source = Block8x8.Load(data); Block8x8F dest = source.AsFloatBlock(); @@ -82,7 +62,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg public void ToArray() { short[] data = Create8x8ShortData(); - var block = new Block8x8(data); + var block = Block8x8.Load(data); short[] result = block.ToArray(); @@ -93,8 +73,8 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg public void Equality_WhenTrue() { short[] data = Create8x8ShortData(); - var block1 = new Block8x8(data); - var block2 = new Block8x8(data); + var block1 = Block8x8.Load(data); + var block2 = Block8x8.Load(data); block1[0] = 42; block2[0] = 42; @@ -107,8 +87,8 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg public void Equality_WhenFalse() { short[] data = Create8x8ShortData(); - var block1 = new Block8x8(data); - var block2 = new Block8x8(data); + var block1 = Block8x8.Load(data); + var block2 = Block8x8.Load(data); block1[0] = 42; block2[0] = 666; @@ -131,8 +111,8 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg public void TotalDifference() { short[] data = Create8x8ShortData(); - var block1 = new Block8x8(data); - var block2 = new Block8x8(data); + var block1 = Block8x8.Load(data); + var block2 = Block8x8.Load(data); block2[10] += 7; block2[63] += 8; diff --git a/tests/ImageSharp.Tests/Formats/Jpg/Utils/LibJpegTools.ComponentData.cs b/tests/ImageSharp.Tests/Formats/Jpg/Utils/LibJpegTools.ComponentData.cs index edb8d457b..560238edb 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/Utils/LibJpegTools.ComponentData.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/Utils/LibJpegTools.ComponentData.cs @@ -53,7 +53,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg.Utils { this.MinVal = Math.Min(this.MinVal, data.Min()); this.MaxVal = Math.Max(this.MaxVal, data.Max()); - this.SpectralBlocks[x, y] = new Block8x8(data); + this.SpectralBlocks[x, y] = Block8x8.Load(data); } public void LoadSpectralStride(Buffer2D data, int strideIndex)