Browse Source

Resize optimizations [skip ci]

Former-commit-id: 16b81044c592de8270bef9eea9aab6fd5eec3e59
Former-commit-id: d08308b5961f2316211479e2cdb598e5cc42a5a9
Former-commit-id: 610d164d45dc5ba680705c4baedcacb32ffe0237
af/merge-core
James Jackson-South 10 years ago
parent
commit
8a6cf0c00e
  1. 19
      src/ImageProcessorCore/Samplers/Processors/CompandingResizeProcessor.cs
  2. 2
      src/ImageProcessorCore/Samplers/Processors/ResamplingWeightedProcessor.cs
  3. 19
      src/ImageProcessorCore/Samplers/Processors/ResizeProcessor.cs
  4. 6
      src/ImageProcessorCore/Samplers/Resamplers/BicubicResampler.cs

19
src/ImageProcessorCore/Samplers/Processors/CompandingResizeProcessor.cs

@ -77,8 +77,7 @@ namespace ImageProcessorCore.Processors
if (targetX <= x && x < targetRight) if (targetX <= x && x < targetRight)
{ {
// X coordinates of source points // X coordinates of source points
int originX = (int)((x - startX) * widthFactor); targetPixels[x, y] = sourcePixels[(int)((x - startX) * widthFactor), originY];
targetPixels[x, y] = sourcePixels[originX, originY];
} }
} }
@ -111,8 +110,7 @@ namespace ImageProcessorCore.Processors
if (x >= 0 && x < width) if (x >= 0 && x < width)
{ {
// Ensure offsets are normalised for cropping and padding. // Ensure offsets are normalised for cropping and padding.
int offsetX = x - startX; Weight[] horizontalValues = this.HorizontalWeights[x - startX].Values;
Weight[] horizontalValues = this.HorizontalWeights[offsetX].Values;
// Destination color components // Destination color components
Vector4 destination = Vector4.Zero; Vector4 destination = Vector4.Zero;
@ -120,10 +118,7 @@ namespace ImageProcessorCore.Processors
for (int i = 0; i < horizontalValues.Length; i++) for (int i = 0; i < horizontalValues.Length; i++)
{ {
Weight xw = horizontalValues[i]; Weight xw = horizontalValues[i];
int originX = xw.Index; destination += sourcePixels[xw.Index, y].ToVector4().Expand() * xw.Value;
Vector4 sourceColor = sourcePixels[originX, y].ToVector4().Expand();
destination += sourceColor * xw.Value;
} }
T d = default(T); T d = default(T);
@ -143,8 +138,7 @@ namespace ImageProcessorCore.Processors
if (y >= 0 && y < height) if (y >= 0 && y < height)
{ {
// Ensure offsets are normalised for cropping and padding. // Ensure offsets are normalised for cropping and padding.
int offsetY = y - startY; Weight[] verticalValues = this.VerticalWeights[y - startY].Values;
Weight[] verticalValues = this.VerticalWeights[offsetY].Values;
for (int x = 0; x < width; x++) for (int x = 0; x < width; x++)
{ {
@ -154,10 +148,7 @@ namespace ImageProcessorCore.Processors
for (int i = 0; i < verticalValues.Length; i++) for (int i = 0; i < verticalValues.Length; i++)
{ {
Weight yw = verticalValues[i]; Weight yw = verticalValues[i];
int originY = yw.Index; destination += firstPassPixels[x, yw.Index].ToVector4().Expand() * yw.Value;
Vector4 sourceColor = firstPassPixels[x, originY].ToVector4().Expand();
destination += sourceColor * yw.Value;
} }
T d = default(T); T d = default(T);

2
src/ImageProcessorCore/Samplers/Processors/ResamplingWeightedProcessor.cs

@ -6,8 +6,6 @@
namespace ImageProcessorCore.Processors namespace ImageProcessorCore.Processors
{ {
using System; using System;
using System.Collections.Generic;
using System.Linq;
/// <summary> /// <summary>
/// Provides methods that allow the resizing of images using various algorithms. /// Provides methods that allow the resizing of images using various algorithms.

19
src/ImageProcessorCore/Samplers/Processors/ResizeProcessor.cs

@ -75,8 +75,7 @@ namespace ImageProcessorCore.Processors
if (targetX <= x && x < targetRight) if (targetX <= x && x < targetRight)
{ {
// X coordinates of source points // X coordinates of source points
int originX = (int)((x - startX) * widthFactor); targetPixels[x, y] = sourcePixels[(int)((x - startX) * widthFactor), originY];
targetPixels[x, y] = sourcePixels[originX, originY];
} }
} }
@ -109,8 +108,7 @@ namespace ImageProcessorCore.Processors
if (x >= 0 && x < width) if (x >= 0 && x < width)
{ {
// Ensure offsets are normalised for cropping and padding. // Ensure offsets are normalised for cropping and padding.
int offsetX = x - startX; Weight[] horizontalValues = this.HorizontalWeights[x - startX].Values;
Weight[] horizontalValues = this.HorizontalWeights[offsetX].Values;
// Destination color components // Destination color components
Vector4 destination = Vector4.Zero; Vector4 destination = Vector4.Zero;
@ -118,10 +116,7 @@ namespace ImageProcessorCore.Processors
for (int i = 0; i < horizontalValues.Length; i++) for (int i = 0; i < horizontalValues.Length; i++)
{ {
Weight xw = horizontalValues[i]; Weight xw = horizontalValues[i];
int originX = xw.Index; destination += sourcePixels[xw.Index, y].ToVector4() * xw.Value;
Vector4 sourceColor = sourcePixels[originX, y].ToVector4();
destination += sourceColor * xw.Value;
} }
T d = default(T); T d = default(T);
@ -141,8 +136,7 @@ namespace ImageProcessorCore.Processors
if (y >= 0 && y < height) if (y >= 0 && y < height)
{ {
// Ensure offsets are normalised for cropping and padding. // Ensure offsets are normalised for cropping and padding.
int offsetY = y - startY; Weight[] verticalValues = this.VerticalWeights[y - startY].Values;
Weight[] verticalValues = this.VerticalWeights[offsetY].Values;
for (int x = 0; x < width; x++) for (int x = 0; x < width; x++)
{ {
@ -152,10 +146,7 @@ namespace ImageProcessorCore.Processors
for (int i = 0; i < verticalValues.Length; i++) for (int i = 0; i < verticalValues.Length; i++)
{ {
Weight yw = verticalValues[i]; Weight yw = verticalValues[i];
int originY = yw.Index; destination += firstPassPixels[x, yw.Index].ToVector4() * yw.Value;
Vector4 sourceColor = firstPassPixels[x, originY].ToVector4();
destination += sourceColor * yw.Value;
} }
T d = default(T); T d = default(T);

6
src/ImageProcessorCore/Samplers/Resamplers/BicubicResampler.cs

@ -18,9 +18,6 @@ namespace ImageProcessorCore
/// <inheritdoc/> /// <inheritdoc/>
public float GetValue(float x) public float GetValue(float x)
{ {
// The coefficient.
float a = -0.5F;
if (x < 0F) if (x < 0F)
{ {
x = -x; x = -x;
@ -28,6 +25,7 @@ namespace ImageProcessorCore
float result = 0; float result = 0;
// Given the coefficient "a" as -0.5F.
if (x <= 1F) if (x <= 1F)
{ {
// Below simplified result = ((a + 2F) * (x * x * x)) - ((a + 3F) * (x * x)) + 1; // Below simplified result = ((a + 2F) * (x * x * x)) - ((a + 3F) * (x * x)) + 1;
@ -36,7 +34,7 @@ namespace ImageProcessorCore
else if (x < 2F) else if (x < 2F)
{ {
// Below simplified result = (a * (x * x * x)) - ((5F * a) * (x * x)) + ((8F * a) * x) - (4F * a); // 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; return result;

Loading…
Cancel
Save