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;
using System.Collections.Generic; using System.Collections.Generic;
using System.Numerics;
using System.Threading.Tasks; using System.Threading.Tasks;
using ImageProcessor.Filters; using ImageProcessor.Filters;
@ -83,8 +84,6 @@ namespace ImageProcessor.Samplers
this.horizontalWeights = this.PrecomputeWeights(targetRectangle.Width, sourceRectangle.Width); this.horizontalWeights = this.PrecomputeWeights(targetRectangle.Width, sourceRectangle.Width);
this.verticalWeights = this.PrecomputeWeights(targetRectangle.Height, sourceRectangle.Height); this.verticalWeights = this.PrecomputeWeights(targetRectangle.Height, sourceRectangle.Height);
} }
new BackgroundColor(Color.Transparent).Apply(source, source, sourceRectangle);
} }
/// <inheritdoc/> /// <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> /// <summary>
/// Resamples the specified <see cref="ImageBase"/> at the specified location /// Resamples the specified <see cref="ImageBase"/> at the specified location
/// and with the specified size. /// and with the specified size.
@ -121,9 +130,9 @@ namespace ImageProcessor.Samplers
/// </remarks> /// </remarks>
private void ApplyResizeOnly(ImageBase target, ImageBase source, Rectangle targetRectangle, int startY, int endY) 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) if (source.Bounds == target.Bounds)
{ {
target.ClonePixels(target.Width, target.Height, source.Pixels);
return; return;
} }
@ -163,6 +172,7 @@ namespace ImageProcessor.Samplers
} }
// Interpolate the image using the calculated weights. // 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( Parallel.For(
startY, startY,
endY, endY,
@ -187,8 +197,7 @@ namespace ImageProcessor.Samplers
{ {
int originX = xw.Index; int originX = xw.Index;
Color sourceColor = Color.Expand(source[originX, originY]); Color sourceColor = Color.Expand(source[originX, originY]);
float weight = yw.Value * xw.Value; destination += sourceColor * yw.Value * xw.Value;
destination += sourceColor * weight;
} }
} }
@ -297,8 +306,7 @@ namespace ImageProcessor.Samplers
if (sourceRectangle.Contains(rotated.X, rotated.Y)) if (sourceRectangle.Contains(rotated.X, rotated.Y))
{ {
Color sourceColor = Color.Expand(source[rotated.X, rotated.Y]); Color sourceColor = Color.Expand(source[rotated.X, rotated.Y]);
float weight = yw.Value * xw.Value; destination += sourceColor * yw.Value * xw.Value;
destination += sourceColor * weight;
} }
} }
} }

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

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

Loading…
Cancel
Save