From 3ffb02844f65f6033647d572d62c5153f98ceb59 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 27 Apr 2017 19:19:23 +1000 Subject: [PATCH] Fix source rectangle regression in resize #118 --- .../ResamplingWeightedProcessor.Weights.cs | 19 +++++++++++-------- .../Processors/Transforms/ResizeProcessor.cs | 8 ++++---- tests/ImageSharp.Tests/FileTestBase.cs | 1 + tests/ImageSharp.Tests/TestImages.cs | 1 + .../TestImages/Formats/Png/cross.png | 3 +++ 5 files changed, 20 insertions(+), 12 deletions(-) create mode 100644 tests/ImageSharp.Tests/TestImages/Formats/Png/cross.png diff --git a/src/ImageSharp/Processing/Processors/Transforms/ResamplingWeightedProcessor.Weights.cs b/src/ImageSharp/Processing/Processors/Transforms/ResamplingWeightedProcessor.Weights.cs index 5d29924e11..f9c78c12fe 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/ResamplingWeightedProcessor.Weights.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/ResamplingWeightedProcessor.Weights.cs @@ -52,13 +52,14 @@ namespace ImageSharp.Processing.Processors /// Computes the sum of vectors in 'rowSpan' weighted by weight values, pointed by this instance. /// /// The input span of vectors + /// The source row position. /// The weighted sum [MethodImpl(MethodImplOptions.AggressiveInlining)] - public Vector4 ComputeWeightedRowSum(BufferSpan rowSpan) + public Vector4 ComputeWeightedRowSum(BufferSpan rowSpan, int sourceX) { ref float horizontalValues = ref this.Ptr; int left = this.Left; - ref Vector4 vecPtr = ref Unsafe.Add(ref rowSpan.DangerousGetPinnableReference(), left); + ref Vector4 vecPtr = ref Unsafe.Add(ref rowSpan.DangerousGetPinnableReference(), left + sourceX); // Destination color components Vector4 result = Vector4.Zero; @@ -78,13 +79,14 @@ namespace ImageSharp.Processing.Processors /// Applies to all input vectors. /// /// The input span of vectors + /// The source row position. /// The weighted sum [MethodImpl(MethodImplOptions.AggressiveInlining)] - public Vector4 ComputeExpandedWeightedRowSum(BufferSpan rowSpan) + public Vector4 ComputeExpandedWeightedRowSum(BufferSpan rowSpan, int sourceX) { ref float horizontalValues = ref this.Ptr; int left = this.Left; - ref Vector4 vecPtr = ref Unsafe.Add(ref rowSpan.DangerousGetPinnableReference(), left); + ref Vector4 vecPtr = ref Unsafe.Add(ref rowSpan.DangerousGetPinnableReference(), left + sourceX); // Destination color components Vector4 result = Vector4.Zero; @@ -100,14 +102,15 @@ namespace ImageSharp.Processing.Processors } /// - /// Computes the sum of vectors in 'firstPassPixels' at a column pointed by 'x', + /// Computes the sum of vectors in 'firstPassPixels' at a row pointed by 'x', /// weighted by weight values, pointed by this instance. /// /// The buffer of input vectors in row first order - /// The column position + /// The row position + /// The source column position. /// The weighted sum [MethodImpl(MethodImplOptions.AggressiveInlining)] - public Vector4 ComputeWeightedColumnSum(Buffer2D firstPassPixels, int x) + public Vector4 ComputeWeightedColumnSum(Buffer2D firstPassPixels, int x, int sourceY) { ref float verticalValues = ref this.Ptr; int left = this.Left; @@ -118,7 +121,7 @@ namespace ImageSharp.Processing.Processors for (int i = 0; i < this.Length; i++) { float yw = Unsafe.Add(ref verticalValues, i); - int index = left + i; + int index = left + i + sourceY; result += firstPassPixels[x, index] * yw; } diff --git a/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs index 23166fd3ab..ecff3da2cd 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs @@ -133,7 +133,7 @@ namespace ImageSharp.Processing.Processors for (int x = minX; x < maxX; x++) { WeightsWindow window = this.HorizontalWeights.Weights[x - startX]; - firstPassPixels[x, y] = window.ComputeExpandedWeightedRowSum(tempRowBuffer); + firstPassPixels[x, y] = window.ComputeExpandedWeightedRowSum(tempRowBuffer, sourceX); } } else @@ -141,7 +141,7 @@ namespace ImageSharp.Processing.Processors for (int x = minX; x < maxX; x++) { WeightsWindow window = this.HorizontalWeights.Weights[x - startX]; - firstPassPixels[x, y] = window.ComputeWeightedRowSum(tempRowBuffer); + firstPassPixels[x, y] = window.ComputeWeightedRowSum(tempRowBuffer, sourceX); } } } @@ -162,7 +162,7 @@ namespace ImageSharp.Processing.Processors for (int x = 0; x < width; x++) { // Destination color components - Vector4 destination = window.ComputeWeightedColumnSum(firstPassPixels, x); + Vector4 destination = window.ComputeWeightedColumnSum(firstPassPixels,x, sourceY); destination = destination.Compress(); TPixel d = default(TPixel); d.PackFromVector4(destination); @@ -174,7 +174,7 @@ namespace ImageSharp.Processing.Processors for (int x = 0; x < width; x++) { // Destination color components - Vector4 destination = window.ComputeWeightedColumnSum(firstPassPixels, x); + Vector4 destination = window.ComputeWeightedColumnSum(firstPassPixels,x, sourceY); TPixel d = default(TPixel); d.PackFromVector4(destination); diff --git a/tests/ImageSharp.Tests/FileTestBase.cs b/tests/ImageSharp.Tests/FileTestBase.cs index 1fa26063da..12c7d51541 100644 --- a/tests/ImageSharp.Tests/FileTestBase.cs +++ b/tests/ImageSharp.Tests/FileTestBase.cs @@ -30,6 +30,7 @@ namespace ImageSharp.Tests TestFile.Create(TestImages.Bmp.Car), // TestFile.Create(TestImages.Bmp.NegHeight), // Perf: Enable for local testing only TestFile.Create(TestImages.Png.Splash), + // TestFile.Create(TestImages.Png.Cross), // Perf: Enable for local testing only // TestFile.Create(TestImages.Png.ChunkLength1), // Perf: Enable for local testing only // TestFile.Create(TestImages.Png.ChunkLength2), // Perf: Enable for local testing only // TestFile.Create(TestImages.Png.Powerpoint), // Perf: Enable for local testing only diff --git a/tests/ImageSharp.Tests/TestImages.cs b/tests/ImageSharp.Tests/TestImages.cs index 5be1240efc..44c8c34ee3 100644 --- a/tests/ImageSharp.Tests/TestImages.cs +++ b/tests/ImageSharp.Tests/TestImages.cs @@ -21,6 +21,7 @@ namespace ImageSharp.Tests public const string Blur = "Png/blur.png"; public const string Indexed = "Png/indexed.png"; public const string Splash = "Png/splash.png"; + public const string Cross = "Png/cross.png"; public const string Powerpoint = "Png/pp.png"; public const string SplashInterlaced = "Png/splash-interlaced.png"; public const string Interlaced = "Png/interlaced.png"; diff --git a/tests/ImageSharp.Tests/TestImages/Formats/Png/cross.png b/tests/ImageSharp.Tests/TestImages/Formats/Png/cross.png new file mode 100644 index 0000000000..1d176fb7b8 --- /dev/null +++ b/tests/ImageSharp.Tests/TestImages/Formats/Png/cross.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0677fbb7f1bd8dd19e8bd7ee802e07f3600193dafbfccf89f43e64e4fdf02d8f +size 15227