Browse Source

weight printing experiments, minor changes, comments

af/merge-core
Anton Firszov 9 years ago
parent
commit
3d7b740839
  1. 9
      src/ImageSharp/Processing/Processors/Transforms/ResamplingWeightedProcessor.Weights.cs
  2. 53
      src/ImageSharp/Processing/Processors/Transforms/ResamplingWeightedProcessor.cs
  3. 31
      tests/ImageSharp.Tests/Processors/Filters/ResizeProfilingBenchmarks.cs

9
src/ImageSharp/Processing/Processors/Transforms/ResamplingWeightedProcessor.Weights.cs

@ -12,7 +12,7 @@ namespace ImageSharp.Processing.Processors
/// <summary>
/// Points to a collection of of weights allocated in <see cref="WeightsBuffer"/>.
/// </summary>
protected unsafe struct WeightsWindow
internal unsafe struct WeightsWindow
{
/// <summary>
/// The local left index position
@ -22,6 +22,8 @@ namespace ImageSharp.Processing.Processors
/// <summary>
/// The span of weights pointing to <see cref="WeightsBuffer"/>.
/// </summary>
// TODO: In the case of switching to official System.Memory and System.Buffers.Primitives this should be System.Buffers.Buffer<T> (formerly Memory<T>), because Span<T> is stack-only!
// see: https://github.com/dotnet/corefxlab/blob/873d35ebed7264e2f9adb556f3b61bebc12395d6/docs/specs/memory.md
public BufferSpan<float> Span;
/// <summary>
@ -129,7 +131,7 @@ namespace ImageSharp.Processing.Processors
/// <summary>
/// Holds the <see cref="WeightsWindow"/> values in an optimized contigous memory region.
/// </summary>
protected class WeightsBuffer : IDisposable
internal class WeightsBuffer : IDisposable
{
private PinnedImageBuffer<float> dataBuffer;
@ -140,8 +142,7 @@ namespace ImageSharp.Processing.Processors
/// <param name="destinationSize">The size of the destination window</param>
public WeightsBuffer(int sourceSize, int destinationSize)
{
this.dataBuffer = new PinnedImageBuffer<float>(sourceSize, destinationSize);
this.dataBuffer.Clear();
this.dataBuffer = PinnedImageBuffer<float>.CreateClean(sourceSize, destinationSize);
this.Weights = new WeightsWindow[destinationSize];
}

53
src/ImageSharp/Processing/Processors/Transforms/ResamplingWeightedProcessor.cs

@ -68,38 +68,14 @@ namespace ImageSharp.Processing.Processors
/// </summary>
protected WeightsBuffer VerticalWeights { get; set; }
/// <inheritdoc/>
protected override void BeforeApply(ImageBase<TColor> source, Rectangle sourceRectangle)
{
if (!(this.Sampler is NearestNeighborResampler))
{
this.HorizontalWeights = this.PrecomputeWeights(
this.ResizeRectangle.Width,
sourceRectangle.Width);
this.VerticalWeights = this.PrecomputeWeights(
this.ResizeRectangle.Height,
sourceRectangle.Height);
}
}
/// <inheritdoc />
protected override void AfterApply(ImageBase<TColor> source, Rectangle sourceRectangle)
{
base.AfterApply(source, sourceRectangle);
this.HorizontalWeights?.Dispose();
this.HorizontalWeights = null;
this.VerticalWeights?.Dispose();
this.VerticalWeights = null;
}
/// <summary>
/// Computes the weights to apply at each pixel when resizing.
/// </summary>
/// <param name="destinationSize">The destination size</param>
/// <param name="sourceSize">The source size</param>
/// <returns>The <see cref="WeightsBuffer"/></returns>
protected unsafe WeightsBuffer PrecomputeWeights(int destinationSize, int sourceSize)
// TODO: Made internal to simplify experimenting with weights data. Make it protected again when finished figuring out how to optimize all the stuff!
internal unsafe WeightsBuffer PrecomputeWeights(int destinationSize, int sourceSize)
{
float ratio = (float)sourceSize / destinationSize;
float scale = ratio;
@ -156,5 +132,30 @@ namespace ImageSharp.Processing.Processors
return result;
}
/// <inheritdoc/>
protected override void BeforeApply(ImageBase<TColor> source, Rectangle sourceRectangle)
{
if (!(this.Sampler is NearestNeighborResampler))
{
this.HorizontalWeights = this.PrecomputeWeights(
this.ResizeRectangle.Width,
sourceRectangle.Width);
this.VerticalWeights = this.PrecomputeWeights(
this.ResizeRectangle.Height,
sourceRectangle.Height);
}
}
/// <inheritdoc />
protected override void AfterApply(ImageBase<TColor> source, Rectangle sourceRectangle)
{
base.AfterApply(source, sourceRectangle);
this.HorizontalWeights?.Dispose();
this.HorizontalWeights = null;
this.VerticalWeights?.Dispose();
this.VerticalWeights = null;
}
}
}

31
tests/ImageSharp.Tests/Processors/Filters/ResizeProfilingBenchmarks.cs

@ -1,5 +1,11 @@
namespace ImageSharp.Tests
{
using System.IO;
using System.Text;
using ImageSharp.Processing;
using ImageSharp.Processing.Processors;
using Xunit;
using Xunit.Abstractions;
@ -26,5 +32,30 @@ namespace ImageSharp.Tests
}
});
}
// [Fact]
public void PrintWeightsData()
{
ResizeProcessor<Color> proc = new ResizeProcessor<Color>(new BicubicResampler(), 200, 200);
ResamplingWeightedProcessor<Color>.WeightsBuffer weights = proc.PrecomputeWeights(200, 500);
StringBuilder bld = new StringBuilder();
foreach (ResamplingWeightedProcessor<Color>.WeightsWindow window in weights.Weights)
{
for (int i = 0; i < window.Length; i++)
{
float value = window.Span[i];
bld.Append(value);
bld.Append("| ");
}
bld.AppendLine();
}
File.WriteAllText("BicubicWeights.MD", bld.ToString());
//this.Output.WriteLine(bld.ToString());
}
}
}
Loading…
Cancel
Save