From 0fb7b923d61da9d3e42efa49db8c87862b4021e8 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Fri, 28 Apr 2017 17:04:01 +1000 Subject: [PATCH 1/6] Internalise and inline Compress/Expand --- src/ImageSharp/Common/Extensions/Vector4Extensions.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ImageSharp/Common/Extensions/Vector4Extensions.cs b/src/ImageSharp/Common/Extensions/Vector4Extensions.cs index 31f3f32ae7..fac33da140 100644 --- a/src/ImageSharp/Common/Extensions/Vector4Extensions.cs +++ b/src/ImageSharp/Common/Extensions/Vector4Extensions.cs @@ -5,7 +5,6 @@ namespace ImageSharp { - using System; using System.Numerics; using System.Runtime.CompilerServices; using ImageSharp.PixelFormats; @@ -13,7 +12,7 @@ namespace ImageSharp /// /// Extension methods for the struct. /// - public static class Vector4Extensions + internal static class Vector4Extensions { /// /// Compresses a linear color signal to its sRGB equivalent. @@ -22,6 +21,7 @@ namespace ImageSharp /// /// The whose signal to compress. /// The . + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector4 Compress(this Vector4 linear) { // TODO: Is there a faster way to do this? @@ -35,6 +35,7 @@ namespace ImageSharp /// /// The whose signal to expand. /// The . + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector4 Expand(this Vector4 gamma) { // TODO: Is there a faster way to do this? From 93f0fd54b3c231b0187345d0a7df4fb229beadf9 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Fri, 28 Apr 2017 17:04:12 +1000 Subject: [PATCH 2/6] Fix Resize benchmarls --- tests/ImageSharp.Benchmarks/Samplers/Resize.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/ImageSharp.Benchmarks/Samplers/Resize.cs b/tests/ImageSharp.Benchmarks/Samplers/Resize.cs index 638a56bf31..932c229bd4 100644 --- a/tests/ImageSharp.Benchmarks/Samplers/Resize.cs +++ b/tests/ImageSharp.Benchmarks/Samplers/Resize.cs @@ -14,6 +14,7 @@ namespace ImageSharp.Benchmarks using CoreSize = ImageSharp.Size; using CoreImage = ImageSharp.Image; + using CoreImageVector = ImageSharp.Image; public class Resize : BenchmarkBase { @@ -50,7 +51,7 @@ namespace ImageSharp.Benchmarks [Benchmark(Description = "ImageSharp Vector Resize")] public CoreSize ResizeCoreVector() { - using (CoreImage image = new CoreImage(2000, 2000)) + using (CoreImageVector image = new CoreImageVector(2000, 2000)) { image.Resize(400, 400); return new CoreSize(image.Width, image.Height); @@ -70,7 +71,7 @@ namespace ImageSharp.Benchmarks [Benchmark(Description = "ImageSharp Vector Compand Resize")] public CoreSize ResizeCoreVectorCompand() { - using (CoreImage image = new CoreImage(2000, 2000)) + using (CoreImageVector image = new CoreImageVector(2000, 2000)) { image.Resize(400, 400, true); return new CoreSize(image.Width, image.Height); From a5a9b299a4cdea3d52aab2b72b1d723c2c278d54 Mon Sep 17 00:00:00 2001 From: Drawaes Date: Fri, 28 Apr 2017 23:32:54 +0100 Subject: [PATCH 3/6] Reduce register pressure --- .../Formats/Jpeg/Components/Block8x8F.cs | 25 ++++++++++++++++++ src/ImageSharp/Formats/Jpeg/Components/DCT.cs | 26 +++++++++---------- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Block8x8F.cs b/src/ImageSharp/Formats/Jpeg/Components/Block8x8F.cs index 2b8c15ab3c..ee2c5b9678 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Block8x8F.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Block8x8F.cs @@ -255,6 +255,31 @@ namespace ImageSharp.Formats.Jpg this.V7R *= scaleVec; } + /// + /// Multiply all elements of the block. + /// + /// Vector to multiply by + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void MultiplyAllInplace(float scaleVec) + { + this.V0L *= scaleVec; + this.V0R *= scaleVec; + this.V1L *= scaleVec; + this.V1R *= scaleVec; + this.V2L *= scaleVec; + this.V2R *= scaleVec; + this.V3L *= scaleVec; + this.V3R *= scaleVec; + this.V4L *= scaleVec; + this.V4R *= scaleVec; + this.V5L *= scaleVec; + this.V5R *= scaleVec; + this.V6L *= scaleVec; + this.V6R *= scaleVec; + this.V7L *= scaleVec; + this.V7R *= scaleVec; + } + /// /// Adds a vector to all elements of the block. /// diff --git a/src/ImageSharp/Formats/Jpeg/Components/DCT.cs b/src/ImageSharp/Formats/Jpeg/Components/DCT.cs index 186a23862f..6f2462cf1d 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/DCT.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/DCT.cs @@ -15,31 +15,31 @@ namespace ImageSharp.Formats.Jpg internal static class DCT { #pragma warning disable SA1310 // FieldNamesMustNotContainUnderscore - private static readonly Vector4 C_1_175876 = new Vector4(1.175876f); + private static readonly float C_1_175876 = 1.175876f; - private static readonly Vector4 C_1_961571 = new Vector4(-1.961571f); + private static readonly float C_1_961571 = -1.961571f; - private static readonly Vector4 C_0_390181 = new Vector4(-0.390181f); + private static readonly float C_0_390181 = -0.390181f; - private static readonly Vector4 C_0_899976 = new Vector4(-0.899976f); + private static readonly float C_0_899976 = -0.899976f; - private static readonly Vector4 C_2_562915 = new Vector4(-2.562915f); + private static readonly float C_2_562915 = -2.562915f; - private static readonly Vector4 C_0_298631 = new Vector4(0.298631f); + private static readonly float C_0_298631 = 0.298631f; - private static readonly Vector4 C_2_053120 = new Vector4(2.053120f); + private static readonly float C_2_053120 = 2.053120f; - private static readonly Vector4 C_3_072711 = new Vector4(3.072711f); + private static readonly float C_3_072711 = 3.072711f; - private static readonly Vector4 C_1_501321 = new Vector4(1.501321f); + private static readonly float C_1_501321 = 1.501321f; - private static readonly Vector4 C_0_541196 = new Vector4(0.541196f); + private static readonly float C_0_541196 = 0.541196f; - private static readonly Vector4 C_1_847759 = new Vector4(-1.847759f); + private static readonly float C_1_847759 = -1.847759f; - private static readonly Vector4 C_0_765367 = new Vector4(0.765367f); + private static readonly float C_0_765367 = 0.765367f; - private static readonly Vector4 C_0_125 = new Vector4(0.1250f); + private static readonly float C_0_125 = 0.1250f; #pragma warning restore SA1310 // FieldNamesMustNotContainUnderscore private static readonly Vector4 InvSqrt2 = new Vector4(0.707107f); From 36674fdfaf172e1d78112635b8f41dd269bdab48 Mon Sep 17 00:00:00 2001 From: Drawaes Date: Sat, 29 Apr 2017 00:23:06 +0100 Subject: [PATCH 4/6] More register pressure removal --- src/ImageSharp/Formats/Jpeg/Components/DCT.cs | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/Components/DCT.cs b/src/ImageSharp/Formats/Jpeg/Components/DCT.cs index 6f2462cf1d..5729fe46d6 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/DCT.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/DCT.cs @@ -216,26 +216,26 @@ namespace ImageSharp.Formats.Jpg d.V0L = c0 + c1; d.V4L = c0 - c1; - Vector4 w0 = new Vector4(0.541196f); - Vector4 w1 = new Vector4(1.306563f); + float w0 = 0.541196f; + float w1 = 1.306563f; d.V2L = (w0 * c2) + (w1 * c3); d.V6L = (w0 * c3) - (w1 * c2); - w0 = new Vector4(1.175876f); - w1 = new Vector4(0.785695f); + w0 = 1.175876f; + w1 = 0.785695f; c3 = (w0 * t4) + (w1 * t7); c0 = (w0 * t7) - (w1 * t4); - w0 = new Vector4(1.387040f); - w1 = new Vector4(0.275899f); + w0 = 1.387040f; + w1 = 0.275899f; c2 = (w0 * t5) + (w1 * t6); c1 = (w0 * t6) - (w1 * t5); d.V3L = c0 - c2; d.V5L = c3 - c1; - Vector4 invsqrt2 = new Vector4(0.707107f); + float invsqrt2 = 0.707107f; c0 = (c0 + c2) * invsqrt2; c3 = (c3 + c1) * invsqrt2; @@ -281,19 +281,19 @@ namespace ImageSharp.Formats.Jpg d.V0R = c0 + c1; d.V4R = c0 - c1; - Vector4 w0 = new Vector4(0.541196f); - Vector4 w1 = new Vector4(1.306563f); + float w0 = 0.541196f; + float w1 = 1.306563f; d.V2R = (w0 * c2) + (w1 * c3); d.V6R = (w0 * c3) - (w1 * c2); - w0 = new Vector4(1.175876f); - w1 = new Vector4(0.785695f); + w0 = 1.175876f; + w1 = 0.785695f; c3 = (w0 * t4) + (w1 * t7); c0 = (w0 * t7) - (w1 * t4); - w0 = new Vector4(1.387040f); - w1 = new Vector4(0.275899f); + w0 = 1.387040f; + w1 = 0.275899f; c2 = (w0 * t5) + (w1 * t6); c1 = (w0 * t6) - (w1 * t5); From eb715cfed3cd7c0bb38018f78e98047baacbd3c6 Mon Sep 17 00:00:00 2001 From: Dirk Lemstra Date: Sat, 29 Apr 2017 11:24:58 +0200 Subject: [PATCH 5/6] Removed old overload and used new one (from #201) in unit test. --- .../Formats/Jpeg/Components/Block8x8F.cs | 25 ------------------- .../Formats/Jpg/Block8x8FTests.cs | 2 +- 2 files changed, 1 insertion(+), 26 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Block8x8F.cs b/src/ImageSharp/Formats/Jpeg/Components/Block8x8F.cs index ee2c5b9678..56466d7a0f 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Block8x8F.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Block8x8F.cs @@ -230,31 +230,6 @@ namespace ImageSharp.Formats.Jpg } } - /// - /// Multiply all elements of the block. - /// - /// Vector to multiply by - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void MultiplyAllInplace(Vector4 scaleVec) - { - this.V0L *= scaleVec; - this.V0R *= scaleVec; - this.V1L *= scaleVec; - this.V1R *= scaleVec; - this.V2L *= scaleVec; - this.V2R *= scaleVec; - this.V3L *= scaleVec; - this.V3R *= scaleVec; - this.V4L *= scaleVec; - this.V4R *= scaleVec; - this.V5L *= scaleVec; - this.V5R *= scaleVec; - this.V6L *= scaleVec; - this.V6R *= scaleVec; - this.V7L *= scaleVec; - this.V7R *= scaleVec; - } - /// /// Multiply all elements of the block. /// diff --git a/tests/ImageSharp.Tests/Formats/Jpg/Block8x8FTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/Block8x8FTests.cs index 63ddbc884c..01501a33d4 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/Block8x8FTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/Block8x8FTests.cs @@ -309,7 +309,7 @@ namespace ImageSharp.Tests float[] data = Create8x8FloatData(); Block8x8F block = new Block8x8F(); block.LoadFrom(data); - block.MultiplyAllInplace(new Vector4(5, 5, 5, 5)); + block.MultiplyAllInplace(5); int stride = 256; int height = 42; From bca48a6f22cbbed202e00ee1f6ac29b4bb0bb7c1 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Sun, 30 Apr 2017 00:57:19 +1000 Subject: [PATCH 6/6] Fix App0 marker Resolution and thumbnail were the wrong way round. --- .../Formats/Jpeg/JpegEncoderCore.cs | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs index 0ce59c6dec..fd70627294 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs @@ -109,7 +109,7 @@ namespace ImageSharp.Formats /// /// A scratch buffer to reduce allocations. /// - private readonly byte[] buffer = new byte[16]; + private readonly byte[] buffer = new byte[20]; /// /// A buffer for reducing the number of stream writes when emitting Huffman tables. 64 seems to be enough. @@ -514,19 +514,17 @@ namespace ImageSharp.Formats this.buffer[12] = 0x01; // versionlo this.buffer[13] = 0x01; // xyunits as dpi - // No thumbnail - this.buffer[14] = 0x00; // Thumbnail width - this.buffer[15] = 0x00; // Thumbnail height - - this.outputStream.Write(this.buffer, 0, 16); - // Resolution. Big Endian - this.buffer[0] = (byte)(horizontalResolution >> 8); - this.buffer[1] = (byte)horizontalResolution; - this.buffer[2] = (byte)(verticalResolution >> 8); - this.buffer[3] = (byte)verticalResolution; + this.buffer[14] = (byte)(horizontalResolution >> 8); + this.buffer[15] = (byte)horizontalResolution; + this.buffer[16] = (byte)(verticalResolution >> 8); + this.buffer[17] = (byte)verticalResolution; - this.outputStream.Write(this.buffer, 0, 4); + // No thumbnail + this.buffer[18] = 0x00; // Thumbnail width + this.buffer[19] = 0x00; // Thumbnail height + + this.outputStream.Write(this.buffer, 0, 20); } ///