Browse Source

Slight optimisations to resampler.

Former-commit-id: b3130ed0f4ba51d538e36ee6cab79e9258d3edc6
Former-commit-id: 1c6c4fdc66ce7aca48330b2649933ff111243322
Former-commit-id: d74265804147e84e4646388b4a16038085c80a29
pull/17/head
James Jackson-South 10 years ago
parent
commit
bac1928a4b
  1. 22
      src/ImageProcessor/Samplers/Resampler.cs
  2. 2
      tests/ImageProcessor.Tests/Processors/Samplers/SamplerTests.cs

22
src/ImageProcessor/Samplers/Resampler.cs

@ -7,6 +7,7 @@ namespace ImageProcessor.Samplers
{
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Threading.Tasks;
using ImageProcessor.Filters;
@ -83,8 +84,6 @@ namespace ImageProcessor.Samplers
this.horizontalWeights = this.PrecomputeWeights(targetRectangle.Width, sourceRectangle.Width);
this.verticalWeights = this.PrecomputeWeights(targetRectangle.Height, sourceRectangle.Height);
}
new BackgroundColor(Color.Transparent).Apply(source, source, sourceRectangle);
}
/// <inheritdoc/>
@ -103,6 +102,16 @@ namespace ImageProcessor.Samplers
}
}
/// <inheritdoc/>
protected override void AfterApply(ImageBase source, ImageBase target, Rectangle targetRectangle, Rectangle sourceRectangle)
{
// Copy the pixels over.
if (source.Bounds == target.Bounds && Math.Abs(this.angle) < 0.001f)
{
target.ClonePixels(target.Width, target.Height, source.Pixels);
}
}
/// <summary>
/// Resamples the specified <see cref="ImageBase"/> at the specified location
/// and with the specified size.
@ -121,9 +130,9 @@ namespace ImageProcessor.Samplers
/// </remarks>
private void ApplyResizeOnly(ImageBase target, ImageBase source, Rectangle targetRectangle, int startY, int endY)
{
// Jump out, we'll deal with that later.
if (source.Bounds == target.Bounds)
{
target.ClonePixels(target.Width, target.Height, source.Pixels);
return;
}
@ -163,6 +172,7 @@ namespace ImageProcessor.Samplers
}
// Interpolate the image using the calculated weights.
// TODO: Figure out a way to split this up so we can reduce complexity and speed things up.
Parallel.For(
startY,
endY,
@ -187,8 +197,7 @@ namespace ImageProcessor.Samplers
{
int originX = xw.Index;
Color sourceColor = Color.Expand(source[originX, originY]);
float weight = yw.Value * xw.Value;
destination += sourceColor * weight;
destination += sourceColor * yw.Value * xw.Value;
}
}
@ -297,8 +306,7 @@ namespace ImageProcessor.Samplers
if (sourceRectangle.Contains(rotated.X, rotated.Y))
{
Color sourceColor = Color.Expand(source[rotated.X, rotated.Y]);
float weight = yw.Value * xw.Value;
destination += sourceColor * weight;
destination += sourceColor * yw.Value * xw.Value;
}
}
}

2
tests/ImageProcessor.Tests/Processors/Samplers/SamplerTests.cs

@ -21,7 +21,7 @@
//{ "Lanczos5", new Lanczos5Resampler() },
//{ "Lanczos8", new Lanczos8Resampler() },
//{ "MitchellNetravali", new MitchellNetravaliResampler() },
//{ "NearestNeighbor", new NearestNeighborResampler() },
{ "NearestNeighbor", new NearestNeighborResampler() },
//{ "Hermite", new HermiteResampler() },
//{ "Spline", new SplineResampler() },
//{ "Robidoux", new RobidouxResampler() },

Loading…
Cancel
Save