Browse Source

Resize

af/merge-core
James Jackson-South 9 years ago
parent
commit
2bd0c2c9ba
  1. 18
      src/ImageSharp/Processing/Transforms/Options/ResizeHelper.cs
  2. 22
      src/ImageSharp/Processing/Transforms/Resize.cs
  3. 35
      tests/ImageSharp.Tests/Processing/Transforms/PadTest.cs
  4. 17
      tests/ImageSharp.Tests/Processing/Transforms/ResizeProfilingBenchmarks.cs
  5. 273
      tests/ImageSharp.Tests/Processing/Transforms/ResizeTests.cs
  6. 31
      tests/ImageSharp.Tests/Processors/Filters/PadTest.cs
  7. 350
      tests/ImageSharp.Tests/Processors/Filters/ResizeTests.cs
  8. 20
      tests/ImageSharp.Tests/TestUtilities/MeasureFixture.cs

18
src/ImageSharp/Processing/Transforms/Options/ResizeHelper.cs

@ -399,6 +399,10 @@ namespace ImageSharp.Processing
return new Rectangle(0, 0, source.Width, source.Height);
}
// Fractional variants for preserving aspect ratio.
float percentHeight = MathF.Abs(height / (float)source.Height);
float percentWidth = MathF.Abs(width / (float)source.Width);
float sourceRatio = (float)source.Height / source.Width;
// Find the shortest distance to go.
@ -419,8 +423,18 @@ namespace ImageSharp.Processing
}
else
{
destinationWidth = width;
destinationHeight = height;
if (height > width)
{
destinationWidth = width;
destinationHeight = Convert.ToInt32(source.Height * percentWidth);
height = destinationHeight;
}
else
{
destinationHeight = height;
destinationWidth = Convert.ToInt32(source.Width * percentHeight);
width = destinationWidth;
}
}
// Replace the size to match the rectangle.

22
src/ImageSharp/Processing/Transforms/Resize.cs

@ -5,8 +5,6 @@
namespace ImageSharp
{
using System;
using ImageSharp.PixelFormats;
using ImageSharp.Processing;
@ -58,6 +56,21 @@ namespace ImageSharp
return Resize(source, size.Width, size.Height, new BicubicResampler(), false);
}
/// <summary>
/// Resizes an image to the given <see cref="Size"/>.
/// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <param name="source">The image to resize.</param>
/// <param name="size">The target image size.</param>
/// <param name="compand">Whether to compress and expand the image color-space to gamma correct the image during processing.</param>
/// <returns>The <see cref="Image{TPixel}"/></returns>
/// <remarks>Passing zero for one of height or width will automatically preserve the aspect ratio of the original image</remarks>
public static Image<TPixel> Resize<TPixel>(this Image<TPixel> source, Size size, bool compand)
where TPixel : struct, IPixel<TPixel>
{
return Resize(source, size.Width, size.Height, new BicubicResampler(), compand);
}
/// <summary>
/// Resizes an image to the given width and height.
/// </summary>
@ -140,7 +153,7 @@ namespace ImageSharp
/// <param name="compand">Whether to compress and expand the image color-space to gamma correct the image during processing.</param>
/// <returns>The <see cref="Image{TPixel}"/></returns>
/// <remarks>Passing zero for one of height or width will automatically preserve the aspect ratio of the original image</remarks>
public static Image<TPixel> Resize<TPixel>(this Image<TPixel> source, int width, int height, IResampler sampler, Rectangle sourceRectangle, Rectangle targetRectangle, bool compand = false)
public static Image<TPixel> Resize<TPixel>(this Image<TPixel> source, int width, int height, IResampler sampler, Rectangle sourceRectangle, Rectangle targetRectangle, bool compand)
where TPixel : struct, IPixel<TPixel>
{
if (width == 0 && height > 0)
@ -158,8 +171,7 @@ namespace ImageSharp
Guard.MustBeGreaterThan(width, 0, nameof(width));
Guard.MustBeGreaterThan(height, 0, nameof(height));
ResizeProcessor<TPixel> processor =
new ResizeProcessor<TPixel>(sampler, width, height, targetRectangle) { Compand = compand };
var processor = new ResizeProcessor<TPixel>(sampler, width, height, targetRectangle) { Compand = compand };
source.ApplyProcessor(processor, sourceRectangle);
return source;

35
tests/ImageSharp.Tests/Processing/Transforms/PadTest.cs

@ -0,0 +1,35 @@
// <copyright file="PadTest.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp.Tests.Processing.Transforms
{
using ImageSharp.PixelFormats;
using Xunit;
public class PadTest : FileTestBase
{
[Theory]
[WithFileCollection(nameof(DefaultFiles), StandardPixelType)]
public void ImageShouldPad<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
image.Pad(image.Width + 50, image.Height + 50)
.DebugSave(provider, null, Extensions.Bmp);
// Check pixels are empty
for (int y = 0; y < 25; y++)
{
for (int x = 0; x < 25; x++)
{
Assert.Equal(image[x, y], default(TPixel));
}
}
}
}
}
}

17
tests/ImageSharp.Tests/Processors/Filters/ResizeProfilingBenchmarks.cs → tests/ImageSharp.Tests/Processing/Transforms/ResizeProfilingBenchmarks.cs

@ -1,9 +1,13 @@
namespace ImageSharp.Tests
// <copyright file="ResizeProfilingBenchmarks.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp.Tests.Processing.Transforms
{
using System.IO;
using System.Text;
using ImageSharp.PixelFormats;
using ImageSharp.Processing;
using ImageSharp.Processing.Processors;
@ -27,7 +31,7 @@ namespace ImageSharp.Tests
this.Measure(this.ExecutionCount,
() =>
{
using (Image<Rgba32> image = new Image<Rgba32>(width, height))
using (var image = new Image<Rgba32>(width, height))
{
image.Resize(width / 4, height / 4);
}
@ -37,11 +41,11 @@ namespace ImageSharp.Tests
// [Fact]
public void PrintWeightsData()
{
ResizeProcessor<Rgba32> proc = new ResizeProcessor<Rgba32>(new BicubicResampler(), 200, 200);
var proc = new ResizeProcessor<Rgba32>(new BicubicResampler(), 200, 200);
ResamplingWeightedProcessor<Rgba32>.WeightsBuffer weights = proc.PrecomputeWeights(200, 500);
StringBuilder bld = new StringBuilder();
var bld = new StringBuilder();
foreach (ResamplingWeightedProcessor<Rgba32>.WeightsWindow window in weights.Weights)
{
@ -51,12 +55,13 @@ namespace ImageSharp.Tests
bld.Append(value);
bld.Append("| ");
}
bld.AppendLine();
}
File.WriteAllText("BicubicWeights.MD", bld.ToString());
//this.Output.WriteLine(bld.ToString());
// this.Output.WriteLine(bld.ToString());
}
}
}

273
tests/ImageSharp.Tests/Processing/Transforms/ResizeTests.cs

@ -0,0 +1,273 @@
// <copyright file="ResizeTests.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp.Tests.Processing.Transforms
{
using ImageSharp.PixelFormats;
using ImageSharp.Processing;
using Xunit;
public class ResizeTests : FileTestBase
{
public static readonly string[] ResizeFiles = { TestImages.Jpeg.Baseline.Calliphora };
public static readonly TheoryData<string, IResampler> ReSamplers =
new TheoryData<string, IResampler>
{
{ "Bicubic", new BicubicResampler() },
{ "Triangle", new TriangleResampler() },
{ "NearestNeighbor", new NearestNeighborResampler() },
{ "Box", new BoxResampler() },
{ "Lanczos3", new Lanczos3Resampler() },
{ "Lanczos5", new Lanczos5Resampler() },
{ "MitchellNetravali", new MitchellNetravaliResampler() },
{ "Lanczos8", new Lanczos8Resampler() },
{ "Hermite", new HermiteResampler() },
{ "Spline", new SplineResampler() },
{ "Robidoux", new RobidouxResampler() },
{ "RobidouxSharp", new RobidouxSharpResampler() },
{ "Welch", new WelchResampler() }
};
[Theory]
[WithFileCollection(nameof(ResizeFiles), nameof(ReSamplers), StandardPixelType)]
public void ImageShouldResize<TPixel>(TestImageProvider<TPixel> provider, string name, IResampler sampler)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
image.Resize(image.Width / 2, image.Height / 2, sampler, true)
.DebugSave(provider, name, Extensions.Bmp);
}
}
[Theory]
[WithFileCollection(nameof(ResizeFiles), nameof(ReSamplers), StandardPixelType)]
public void ImageShouldResizeFromSourceRectangle<TPixel>(TestImageProvider<TPixel> provider, string name, IResampler sampler)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
var sourceRectangle = new Rectangle(image.Width / 8, image.Height / 8, image.Width / 4, image.Height / 4);
var destRectangle = new Rectangle(image.Width / 4, image.Height / 4, image.Width / 2, image.Height / 2);
image.Resize(image.Width, image.Height, sampler, sourceRectangle, destRectangle, false)
.DebugSave(provider, name, Extensions.Bmp);
}
}
[Theory]
[WithFileCollection(nameof(ResizeFiles), nameof(ReSamplers), StandardPixelType)]
public void ImageShouldResizeWidthAndKeepAspect<TPixel>(TestImageProvider<TPixel> provider, string name, IResampler sampler)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
image.Resize(image.Width / 3, 0, sampler, false)
.DebugSave(provider, name, Extensions.Bmp);
}
}
[Theory]
[WithFileCollection(nameof(ResizeFiles), nameof(ReSamplers), StandardPixelType)]
public void ImageShouldResizeHeightAndKeepAspect<TPixel>(TestImageProvider<TPixel> provider, string name, IResampler sampler)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
image.Resize(0, image.Height / 3, sampler, false)
.DebugSave(provider, name, Extensions.Bmp);
}
}
[Theory]
[WithFileCollection(nameof(ResizeFiles), nameof(ReSamplers), StandardPixelType)]
public void ImageShouldResizeWithCropWidthMode<TPixel>(TestImageProvider<TPixel> provider, string name, IResampler sampler)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
var options = new ResizeOptions
{
Sampler = sampler,
Size = new Size(image.Width / 2, image.Height)
};
image.Resize(options)
.DebugSave(provider, name, Extensions.Bmp);
}
}
[Theory]
[WithFileCollection(nameof(ResizeFiles), nameof(ReSamplers), StandardPixelType)]
public void ImageShouldResizeWithCropHeightMode<TPixel>(TestImageProvider<TPixel> provider, string name, IResampler sampler)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
var options = new ResizeOptions
{
Sampler = sampler,
Size = new Size(image.Width, image.Height / 2)
};
image.Resize(options)
.DebugSave(provider, name, Extensions.Bmp);
}
}
[Theory]
[WithFileCollection(nameof(ResizeFiles), nameof(ReSamplers), StandardPixelType)]
public void ImageShouldResizeWithPadMode<TPixel>(TestImageProvider<TPixel> provider, string name, IResampler sampler)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
var options = new ResizeOptions
{
Sampler = sampler,
Size = new Size(image.Width + 200, image.Height),
Mode = ResizeMode.Pad
};
image.Resize(options)
.DebugSave(provider, name, Extensions.Bmp);
}
}
[Theory]
[WithFileCollection(nameof(ResizeFiles), nameof(ReSamplers), StandardPixelType)]
public void ImageShouldResizeWithBoxPadMode<TPixel>(TestImageProvider<TPixel> provider, string name, IResampler sampler)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
var options = new ResizeOptions
{
Sampler = sampler,
Size = new Size(image.Width + 200, image.Height + 200),
Mode = ResizeMode.BoxPad
};
image.Resize(options)
.DebugSave(provider, name, Extensions.Bmp);
}
}
[Theory]
[WithFileCollection(nameof(ResizeFiles), nameof(ReSamplers), StandardPixelType)]
public void ImageShouldResizeWithMaxMode<TPixel>(TestImageProvider<TPixel> provider, string name, IResampler sampler)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
var options = new ResizeOptions
{
Sampler = sampler,
Size = new Size(300, 300),
Mode = ResizeMode.Max
};
image.Resize(options)
.DebugSave(provider, name, Extensions.Bmp);
}
}
[Theory]
[WithFileCollection(nameof(ResizeFiles), nameof(ReSamplers), StandardPixelType)]
public void ImageShouldResizeWithMinMode<TPixel>(TestImageProvider<TPixel> provider, string name, IResampler sampler)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
var options = new ResizeOptions
{
Sampler = sampler,
Size = new Size((int)MathF.Round(image.Width * .75F), (int)MathF.Round(image.Height * .95F)),
Mode = ResizeMode.Min
};
image.Resize(options)
.DebugSave(provider, name, Extensions.Bmp);
}
}
[Theory]
[WithFileCollection(nameof(ResizeFiles), nameof(ReSamplers), StandardPixelType)]
public void ImageShouldResizeWithStretchMode<TPixel>(TestImageProvider<TPixel> provider, string name, IResampler sampler)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
var options = new ResizeOptions
{
Sampler = sampler,
Size = new Size(image.Width / 2, image.Height),
Mode = ResizeMode.Stretch
};
image.Resize(options)
.DebugSave(provider, name, Extensions.Bmp);
}
}
[Theory]
[InlineData(-2, 0)]
[InlineData(-1, 0)]
[InlineData(0, 1)]
[InlineData(1, 0)]
[InlineData(2, 0)]
public static void BicubicWindowOscillatesCorrectly(float x, float expected)
{
var sampler = new BicubicResampler();
float result = sampler.GetValue(x);
Assert.Equal(result, expected);
}
[Theory]
[InlineData(-2, 0)]
[InlineData(-1, 0)]
[InlineData(0, 1)]
[InlineData(1, 0)]
[InlineData(2, 0)]
public static void TriangleWindowOscillatesCorrectly(float x, float expected)
{
var sampler = new TriangleResampler();
float result = sampler.GetValue(x);
Assert.Equal(result, expected);
}
[Theory]
[InlineData(-2, 0)]
[InlineData(-1, 0)]
[InlineData(0, 1)]
[InlineData(1, 0)]
[InlineData(2, 0)]
public static void Lanczos3WindowOscillatesCorrectly(float x, float expected)
{
var sampler = new Lanczos3Resampler();
float result = sampler.GetValue(x);
Assert.Equal(result, expected);
}
[Theory]
[InlineData(-4, 0)]
[InlineData(-2, 0)]
[InlineData(0, 1)]
[InlineData(2, 0)]
[InlineData(4, 0)]
public static void Lanczos5WindowOscillatesCorrectly(float x, float expected)
{
var sampler = new Lanczos5Resampler();
float result = sampler.GetValue(x);
Assert.Equal(result, expected);
}
}
}

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

@ -1,31 +0,0 @@
// <copyright file="PadTest.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp.Tests
{
using System.IO;
using ImageSharp.PixelFormats;
using Xunit;
public class PadTest : FileTestBase
{
[Fact]
public void ImageShouldApplyPadSampler()
{
string path = this.CreateOutputDirectory("Pad");
foreach (TestFile file in Files)
{
using (Image<Rgba32> image = file.CreateImage())
using (FileStream output = File.OpenWrite($"{path}/{file.FileName}"))
{
image.Pad(image.Width + 50, image.Height + 50).Save(output);
}
}
}
}
}

350
tests/ImageSharp.Tests/Processors/Filters/ResizeTests.cs

@ -1,350 +0,0 @@
// <copyright file="ResizeTests.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp.Tests
{
using System;
using System.IO;
using ImageSharp.PixelFormats;
using ImageSharp.Processing;
using Xunit;
public class ResizeTests : FileTestBase
{
public static readonly TheoryData<string, IResampler> ReSamplers =
new TheoryData<string, IResampler>
{
{ "Bicubic", new BicubicResampler() },
{ "Triangle", new TriangleResampler() },
{ "NearestNeighbor", new NearestNeighborResampler() },
// Perf: Enable for local testing only
// { "Box", new BoxResampler() },
// { "Lanczos3", new Lanczos3Resampler() },
// { "Lanczos5", new Lanczos5Resampler() },
{ "MitchellNetravali", new MitchellNetravaliResampler() },
// { "Lanczos8", new Lanczos8Resampler() },
// { "Hermite", new HermiteResampler() },
// { "Spline", new SplineResampler() },
// { "Robidoux", new RobidouxResampler() },
// { "RobidouxSharp", new RobidouxSharpResampler() },
// { "RobidouxSoft", new RobidouxSoftResampler() },
// { "Welch", new WelchResampler() }
};
[Theory]
[MemberData(nameof(ReSamplers))]
public void ImageShouldResize(string name, IResampler sampler)
{
string path = this.CreateOutputDirectory("Resize");
foreach (TestFile file in Files)
{
string filename = file.GetFileName(name);
using (Image<Rgba32> image = file.CreateImage())
using (FileStream output = File.OpenWrite($"{path}/{filename}"))
{
image.Resize(image.Width / 2, image.Height / 2, sampler, true).Save(output);
}
}
}
[Theory]
[MemberData(nameof(ReSamplers))]
public void ImageShouldResizeFromSourceRectangle(string name, IResampler sampler)
{
name = $"{name}-SourceRect";
string path = this.CreateOutputDirectory("Resize");
foreach (TestFile file in Files)
{
string filename = file.GetFileName(name);
using (Image<Rgba32> image = file.CreateImage())
using (FileStream output = File.OpenWrite($"{path}/{filename}"))
{
Rectangle sourceRectangle = new Rectangle(image.Width / 8, image.Height / 8, image.Width / 4, image.Height / 4);
Rectangle destRectangle = new Rectangle(image.Width / 4, image.Height / 4, image.Width / 2, image.Height / 2);
image.Resize(image.Width, image.Height, sampler, sourceRectangle, destRectangle, false).Save(output);
}
}
}
[Theory]
[MemberData(nameof(ReSamplers))]
public void ImageShouldResizeWidthAndKeepAspect(string name, IResampler sampler)
{
name = $"{name}-FixedWidth";
string path = this.CreateOutputDirectory("Resize");
foreach (TestFile file in Files)
{
string filename = file.GetFileName(name);
using (Image<Rgba32> image = file.CreateImage())
using (FileStream output = File.OpenWrite($"{path}/{filename}"))
{
image.Resize(image.Width / 3, 0, sampler, false).Save(output);
}
}
}
[Theory]
[MemberData(nameof(ReSamplers))]
public void ImageShouldResizeHeightAndKeepAspect(string name, IResampler sampler)
{
name = $"{name}-FixedHeight";
string path = this.CreateOutputDirectory("Resize");
foreach (TestFile file in Files)
{
string filename = file.GetFileName(name);
using (Image<Rgba32> image = file.CreateImage())
using (FileStream output = File.OpenWrite($"{path}/{filename}"))
{
image.Resize(0, image.Height / 3, sampler, false).Save(output);
}
}
}
[Theory]
[MemberData(nameof(ReSamplers))]
public void ImageShouldResizeWithCropWidthMode(string name, IResampler sampler)
{
name = $"{name}-CropWidth";
string path = this.CreateOutputDirectory("Resize");
foreach (TestFile file in Files)
{
string filename = file.GetFileName(name);
using (Image<Rgba32> image = file.CreateImage())
using (FileStream output = File.OpenWrite($"{path}/{filename}"))
{
ResizeOptions options = new ResizeOptions
{
Sampler = sampler,
Size = new Size(image.Width / 2, image.Height)
};
image.Resize(options).Save(output);
}
}
}
[Theory]
[MemberData(nameof(ReSamplers))]
public void ImageShouldResizeWithCropHeightMode(string name, IResampler sampler)
{
name = $"{name}-CropHeight";
string path = this.CreateOutputDirectory("Resize");
foreach (TestFile file in Files)
{
string filename = file.GetFileName(name);
using (Image<Rgba32> image = file.CreateImage())
using (FileStream output = File.OpenWrite($"{path}/{filename}"))
{
ResizeOptions options = new ResizeOptions
{
Sampler = sampler,
Size = new Size(image.Width, image.Height / 2)
};
image.Resize(options).Save(output);
}
}
}
[Theory]
[MemberData(nameof(ReSamplers))]
public void ImageShouldResizeWithPadMode(string name, IResampler sampler)
{
name = $"{name}-Pad";
string path = this.CreateOutputDirectory("Resize");
foreach (TestFile file in Files)
{
string filename = file.GetFileName(name);
using (Image<Rgba32> image = file.CreateImage())
using (FileStream output = File.OpenWrite($"{path}/{filename}"))
{
ResizeOptions options = new ResizeOptions
{
Size = new Size(image.Width + 200, image.Height),
Mode = ResizeMode.Pad
};
image.Resize(options).Save(output);
}
}
}
[Theory]
[MemberData(nameof(ReSamplers))]
public void ImageShouldResizeWithBoxPadMode(string name, IResampler sampler)
{
name = $"{name}-BoxPad";
string path = this.CreateOutputDirectory("Resize");
foreach (TestFile file in Files)
{
string filename = file.GetFileName(name);
using (Image<Rgba32> image = file.CreateImage())
using (FileStream output = File.OpenWrite($"{path}/{filename}"))
{
ResizeOptions options = new ResizeOptions
{
Sampler = sampler,
Size = new Size(image.Width + 200, image.Height + 200),
Mode = ResizeMode.BoxPad
};
image.Resize(options).Save(output);
}
}
}
[Theory]
[MemberData(nameof(ReSamplers))]
public void ImageShouldResizeWithMaxMode(string name, IResampler sampler)
{
name = $"{name}Max";
string path = this.CreateOutputDirectory("Resize");
foreach (TestFile file in Files)
{
string filename = file.GetFileName(name);
using (Image<Rgba32> image = file.CreateImage())
using (FileStream output = File.OpenWrite($"{path}/{filename}"))
{
ResizeOptions options = new ResizeOptions
{
Sampler = sampler,
Size = new Size(300, 300),
Mode = ResizeMode.Max
};
image.Resize(options).Save(output);
}
}
}
[Theory]
[MemberData(nameof(ReSamplers))]
public void ImageShouldResizeWithMinMode(string name, IResampler sampler)
{
name = $"{name}-Min";
string path = this.CreateOutputDirectory("Resize");
foreach (TestFile file in Files)
{
string filename = file.GetFileName(name);
using (Image<Rgba32> image = file.CreateImage())
using (FileStream output = File.OpenWrite($"{path}/{filename}"))
{
ResizeOptions options = new ResizeOptions
{
Sampler = sampler,
Size = new Size((int)Math.Round(image.Width * .75F), (int)Math.Round(image.Height * .95F)),
Mode = ResizeMode.Min
};
image.Resize(options).Save(output);
}
}
}
[Theory]
[MemberData(nameof(ReSamplers))]
public void ImageShouldResizeWithStretchMode(string name, IResampler sampler)
{
name = $"{name}Stretch";
string path = this.CreateOutputDirectory("Resize");
foreach (TestFile file in Files)
{
string filename = file.GetFileName(name);
using (Image<Rgba32> image = file.CreateImage())
using (FileStream output = File.OpenWrite($"{path}/{filename}"))
{
ResizeOptions options = new ResizeOptions
{
Sampler = sampler,
Size = new Size(image.Width / 2, image.Height),
Mode = ResizeMode.Stretch
};
image.Resize(options).Save(output);
}
}
}
[Theory]
[InlineData(-2, 0)]
[InlineData(-1, 0)]
[InlineData(0, 1)]
[InlineData(1, 0)]
[InlineData(2, 0)]
public static void BicubicWindowOscillatesCorrectly(float x, float expected)
{
BicubicResampler sampler = new BicubicResampler();
float result = sampler.GetValue(x);
Assert.Equal(result, expected);
}
[Theory]
[InlineData(-2, 0)]
[InlineData(-1, 0)]
[InlineData(0, 1)]
[InlineData(1, 0)]
[InlineData(2, 0)]
public static void TriangleWindowOscillatesCorrectly(float x, float expected)
{
TriangleResampler sampler = new TriangleResampler();
float result = sampler.GetValue(x);
Assert.Equal(result, expected);
}
[Theory]
[InlineData(-2, 0)]
[InlineData(-1, 0)]
[InlineData(0, 1)]
[InlineData(1, 0)]
[InlineData(2, 0)]
public static void Lanczos3WindowOscillatesCorrectly(float x, float expected)
{
Lanczos3Resampler sampler = new Lanczos3Resampler();
float result = sampler.GetValue(x);
Assert.Equal(result, expected);
}
[Theory]
[InlineData(-4, 0)]
[InlineData(-2, 0)]
[InlineData(0, 1)]
[InlineData(2, 0)]
[InlineData(4, 0)]
public static void Lanczos5WindowOscillatesCorrectly(float x, float expected)
{
Lanczos5Resampler sampler = new Lanczos5Resampler();
float result = sampler.GetValue(x);
Assert.Equal(result, expected);
}
}
}

20
tests/ImageSharp.Tests/TestUtilities/MeasureFixture.cs

@ -1,4 +1,9 @@
namespace ImageSharp.Tests
// <copyright file="MeasureFixture.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp.Tests
{
using System;
using System.Diagnostics;
@ -24,8 +29,12 @@
/// <param name="operationName">The name of the operation to print to the output</param>
public void Measure(int times, Action action, [CallerMemberName] string operationName = null)
{
if (this.EnablePrinting) this.Output?.WriteLine($"{operationName} X {times} ...");
Stopwatch sw = Stopwatch.StartNew();
if (this.EnablePrinting)
{
this.Output?.WriteLine($"{operationName} X {times} ...");
}
var sw = Stopwatch.StartNew();
for (int i = 0; i < times; i++)
{
@ -33,7 +42,10 @@
}
sw.Stop();
if (this.EnablePrinting) this.Output?.WriteLine($"{operationName} finished in {sw.ElapsedMilliseconds} ms");
if (this.EnablePrinting)
{
this.Output?.WriteLine($"{operationName} finished in {sw.ElapsedMilliseconds} ms");
}
}
/// <summary>

Loading…
Cancel
Save