From 8a6cf0c00e5540ff947b9ec65c10e79faec3c210 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Mon, 8 Aug 2016 23:51:38 +1000 Subject: [PATCH] Resize optimizations [skip ci] Former-commit-id: 16b81044c592de8270bef9eea9aab6fd5eec3e59 Former-commit-id: d08308b5961f2316211479e2cdb598e5cc42a5a9 Former-commit-id: 610d164d45dc5ba680705c4baedcacb32ffe0237 --- .../Processors/CompandingResizeProcessor.cs | 19 +++++-------------- .../Processors/ResamplingWeightedProcessor.cs | 2 -- .../Samplers/Processors/ResizeProcessor.cs | 19 +++++-------------- .../Samplers/Resamplers/BicubicResampler.cs | 6 ++---- 4 files changed, 12 insertions(+), 34 deletions(-) diff --git a/src/ImageProcessorCore/Samplers/Processors/CompandingResizeProcessor.cs b/src/ImageProcessorCore/Samplers/Processors/CompandingResizeProcessor.cs index 9ebdd8917f..409c26878a 100644 --- a/src/ImageProcessorCore/Samplers/Processors/CompandingResizeProcessor.cs +++ b/src/ImageProcessorCore/Samplers/Processors/CompandingResizeProcessor.cs @@ -77,8 +77,7 @@ namespace ImageProcessorCore.Processors if (targetX <= x && x < targetRight) { // X coordinates of source points - int originX = (int)((x - startX) * widthFactor); - targetPixels[x, y] = sourcePixels[originX, originY]; + targetPixels[x, y] = sourcePixels[(int)((x - startX) * widthFactor), originY]; } } @@ -111,8 +110,7 @@ namespace ImageProcessorCore.Processors if (x >= 0 && x < width) { // Ensure offsets are normalised for cropping and padding. - int offsetX = x - startX; - Weight[] horizontalValues = this.HorizontalWeights[offsetX].Values; + Weight[] horizontalValues = this.HorizontalWeights[x - startX].Values; // Destination color components Vector4 destination = Vector4.Zero; @@ -120,10 +118,7 @@ namespace ImageProcessorCore.Processors for (int i = 0; i < horizontalValues.Length; i++) { Weight xw = horizontalValues[i]; - int originX = xw.Index; - Vector4 sourceColor = sourcePixels[originX, y].ToVector4().Expand(); - - destination += sourceColor * xw.Value; + destination += sourcePixels[xw.Index, y].ToVector4().Expand() * xw.Value; } T d = default(T); @@ -143,8 +138,7 @@ namespace ImageProcessorCore.Processors if (y >= 0 && y < height) { // Ensure offsets are normalised for cropping and padding. - int offsetY = y - startY; - Weight[] verticalValues = this.VerticalWeights[offsetY].Values; + Weight[] verticalValues = this.VerticalWeights[y - startY].Values; for (int x = 0; x < width; x++) { @@ -154,10 +148,7 @@ namespace ImageProcessorCore.Processors for (int i = 0; i < verticalValues.Length; i++) { Weight yw = verticalValues[i]; - int originY = yw.Index; - Vector4 sourceColor = firstPassPixels[x, originY].ToVector4().Expand(); - - destination += sourceColor * yw.Value; + destination += firstPassPixels[x, yw.Index].ToVector4().Expand() * yw.Value; } T d = default(T); diff --git a/src/ImageProcessorCore/Samplers/Processors/ResamplingWeightedProcessor.cs b/src/ImageProcessorCore/Samplers/Processors/ResamplingWeightedProcessor.cs index 534e96aa2a..70e9b16631 100644 --- a/src/ImageProcessorCore/Samplers/Processors/ResamplingWeightedProcessor.cs +++ b/src/ImageProcessorCore/Samplers/Processors/ResamplingWeightedProcessor.cs @@ -6,8 +6,6 @@ namespace ImageProcessorCore.Processors { using System; - using System.Collections.Generic; - using System.Linq; /// /// Provides methods that allow the resizing of images using various algorithms. diff --git a/src/ImageProcessorCore/Samplers/Processors/ResizeProcessor.cs b/src/ImageProcessorCore/Samplers/Processors/ResizeProcessor.cs index eae8bf1b9b..0dc72dee3d 100644 --- a/src/ImageProcessorCore/Samplers/Processors/ResizeProcessor.cs +++ b/src/ImageProcessorCore/Samplers/Processors/ResizeProcessor.cs @@ -75,8 +75,7 @@ namespace ImageProcessorCore.Processors if (targetX <= x && x < targetRight) { // X coordinates of source points - int originX = (int)((x - startX) * widthFactor); - targetPixels[x, y] = sourcePixels[originX, originY]; + targetPixels[x, y] = sourcePixels[(int)((x - startX) * widthFactor), originY]; } } @@ -109,8 +108,7 @@ namespace ImageProcessorCore.Processors if (x >= 0 && x < width) { // Ensure offsets are normalised for cropping and padding. - int offsetX = x - startX; - Weight[] horizontalValues = this.HorizontalWeights[offsetX].Values; + Weight[] horizontalValues = this.HorizontalWeights[x - startX].Values; // Destination color components Vector4 destination = Vector4.Zero; @@ -118,10 +116,7 @@ namespace ImageProcessorCore.Processors for (int i = 0; i < horizontalValues.Length; i++) { Weight xw = horizontalValues[i]; - int originX = xw.Index; - Vector4 sourceColor = sourcePixels[originX, y].ToVector4(); - - destination += sourceColor * xw.Value; + destination += sourcePixels[xw.Index, y].ToVector4() * xw.Value; } T d = default(T); @@ -141,8 +136,7 @@ namespace ImageProcessorCore.Processors if (y >= 0 && y < height) { // Ensure offsets are normalised for cropping and padding. - int offsetY = y - startY; - Weight[] verticalValues = this.VerticalWeights[offsetY].Values; + Weight[] verticalValues = this.VerticalWeights[y - startY].Values; for (int x = 0; x < width; x++) { @@ -152,10 +146,7 @@ namespace ImageProcessorCore.Processors for (int i = 0; i < verticalValues.Length; i++) { Weight yw = verticalValues[i]; - int originY = yw.Index; - Vector4 sourceColor = firstPassPixels[x, originY].ToVector4(); - - destination += sourceColor * yw.Value; + destination += firstPassPixels[x, yw.Index].ToVector4() * yw.Value; } T d = default(T); diff --git a/src/ImageProcessorCore/Samplers/Resamplers/BicubicResampler.cs b/src/ImageProcessorCore/Samplers/Resamplers/BicubicResampler.cs index 752c12d6d6..17e886d30f 100644 --- a/src/ImageProcessorCore/Samplers/Resamplers/BicubicResampler.cs +++ b/src/ImageProcessorCore/Samplers/Resamplers/BicubicResampler.cs @@ -18,9 +18,6 @@ namespace ImageProcessorCore /// public float GetValue(float x) { - // The coefficient. - float a = -0.5F; - if (x < 0F) { x = -x; @@ -28,6 +25,7 @@ namespace ImageProcessorCore float result = 0; + // Given the coefficient "a" as -0.5F. if (x <= 1F) { // Below simplified result = ((a + 2F) * (x * x * x)) - ((a + 3F) * (x * x)) + 1; @@ -36,7 +34,7 @@ namespace ImageProcessorCore else if (x < 2F) { // Below simplified result = (a * (x * x * x)) - ((5F * a) * (x * x)) + ((8F * a) * x) - (4F * a); - result = (((((a * x) + 2.5F) * x) - 4) * x) + 2; + result = (((((-0.5F * x) + 2.5F) * x) - 4) * x) + 2; } return result;