diff --git a/src/ImageSharp/Processing/Transforms/Options/ResizeHelper.cs b/src/ImageSharp/Processing/Transforms/Options/ResizeHelper.cs
index c87688266..d9b9b5556 100644
--- a/src/ImageSharp/Processing/Transforms/Options/ResizeHelper.cs
+++ b/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.
diff --git a/src/ImageSharp/Processing/Transforms/Resize.cs b/src/ImageSharp/Processing/Transforms/Resize.cs
index 0ed682648..271a111ca 100644
--- a/src/ImageSharp/Processing/Transforms/Resize.cs
+++ b/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);
}
+ ///
+ /// Resizes an image to the given .
+ ///
+ /// The pixel format.
+ /// The image to resize.
+ /// The target image size.
+ /// Whether to compress and expand the image color-space to gamma correct the image during processing.
+ /// The
+ /// Passing zero for one of height or width will automatically preserve the aspect ratio of the original image
+ public static Image Resize(this Image source, Size size, bool compand)
+ where TPixel : struct, IPixel
+ {
+ return Resize(source, size.Width, size.Height, new BicubicResampler(), compand);
+ }
+
///
/// Resizes an image to the given width and height.
///
@@ -140,7 +153,7 @@ namespace ImageSharp
/// Whether to compress and expand the image color-space to gamma correct the image during processing.
/// The
/// Passing zero for one of height or width will automatically preserve the aspect ratio of the original image
- public static Image Resize(this Image source, int width, int height, IResampler sampler, Rectangle sourceRectangle, Rectangle targetRectangle, bool compand = false)
+ public static Image Resize(this Image source, int width, int height, IResampler sampler, Rectangle sourceRectangle, Rectangle targetRectangle, bool compand)
where TPixel : struct, IPixel
{
if (width == 0 && height > 0)
@@ -158,8 +171,7 @@ namespace ImageSharp
Guard.MustBeGreaterThan(width, 0, nameof(width));
Guard.MustBeGreaterThan(height, 0, nameof(height));
- ResizeProcessor processor =
- new ResizeProcessor(sampler, width, height, targetRectangle) { Compand = compand };
+ var processor = new ResizeProcessor(sampler, width, height, targetRectangle) { Compand = compand };
source.ApplyProcessor(processor, sourceRectangle);
return source;
diff --git a/tests/ImageSharp.Tests/Processing/Transforms/PadTest.cs b/tests/ImageSharp.Tests/Processing/Transforms/PadTest.cs
new file mode 100644
index 000000000..ee03193ae
--- /dev/null
+++ b/tests/ImageSharp.Tests/Processing/Transforms/PadTest.cs
@@ -0,0 +1,35 @@
+//
+// Copyright (c) James Jackson-South and contributors.
+// Licensed under the Apache License, Version 2.0.
+//
+
+namespace ImageSharp.Tests.Processing.Transforms
+{
+ using ImageSharp.PixelFormats;
+
+ using Xunit;
+
+ public class PadTest : FileTestBase
+ {
+ [Theory]
+ [WithFileCollection(nameof(DefaultFiles), StandardPixelType)]
+ public void ImageShouldPad(TestImageProvider provider)
+ where TPixel : struct, IPixel
+ {
+ using (Image 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));
+ }
+ }
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/tests/ImageSharp.Tests/Processors/Filters/ResizeProfilingBenchmarks.cs b/tests/ImageSharp.Tests/Processing/Transforms/ResizeProfilingBenchmarks.cs
similarity index 74%
rename from tests/ImageSharp.Tests/Processors/Filters/ResizeProfilingBenchmarks.cs
rename to tests/ImageSharp.Tests/Processing/Transforms/ResizeProfilingBenchmarks.cs
index a743665d4..3f8a75b92 100644
--- a/tests/ImageSharp.Tests/Processors/Filters/ResizeProfilingBenchmarks.cs
+++ b/tests/ImageSharp.Tests/Processing/Transforms/ResizeProfilingBenchmarks.cs
@@ -1,9 +1,13 @@
-namespace ImageSharp.Tests
+//
+// Copyright (c) James Jackson-South and contributors.
+// Licensed under the Apache License, Version 2.0.
+//
+
+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 image = new Image(width, height))
+ using (var image = new Image(width, height))
{
image.Resize(width / 4, height / 4);
}
@@ -37,11 +41,11 @@ namespace ImageSharp.Tests
// [Fact]
public void PrintWeightsData()
{
- ResizeProcessor proc = new ResizeProcessor(new BicubicResampler(), 200, 200);
+ var proc = new ResizeProcessor(new BicubicResampler(), 200, 200);
ResamplingWeightedProcessor.WeightsBuffer weights = proc.PrecomputeWeights(200, 500);
- StringBuilder bld = new StringBuilder();
+ var bld = new StringBuilder();
foreach (ResamplingWeightedProcessor.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());
}
}
}
\ No newline at end of file
diff --git a/tests/ImageSharp.Tests/Processing/Transforms/ResizeTests.cs b/tests/ImageSharp.Tests/Processing/Transforms/ResizeTests.cs
new file mode 100644
index 000000000..8244f17c3
--- /dev/null
+++ b/tests/ImageSharp.Tests/Processing/Transforms/ResizeTests.cs
@@ -0,0 +1,273 @@
+//
+// Copyright (c) James Jackson-South and contributors.
+// Licensed under the Apache License, Version 2.0.
+//
+
+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 ReSamplers =
+ new TheoryData
+ {
+ { "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(TestImageProvider provider, string name, IResampler sampler)
+ where TPixel : struct, IPixel
+ {
+ using (Image 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(TestImageProvider provider, string name, IResampler sampler)
+ where TPixel : struct, IPixel
+ {
+ using (Image 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(TestImageProvider provider, string name, IResampler sampler)
+ where TPixel : struct, IPixel
+ {
+ using (Image 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(TestImageProvider provider, string name, IResampler sampler)
+ where TPixel : struct, IPixel
+ {
+ using (Image 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(TestImageProvider provider, string name, IResampler sampler)
+ where TPixel : struct, IPixel
+ {
+ using (Image 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(TestImageProvider provider, string name, IResampler sampler)
+ where TPixel : struct, IPixel
+ {
+ using (Image 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(TestImageProvider provider, string name, IResampler sampler)
+ where TPixel : struct, IPixel
+ {
+ using (Image 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(TestImageProvider provider, string name, IResampler sampler)
+ where TPixel : struct, IPixel
+ {
+ using (Image 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(TestImageProvider provider, string name, IResampler sampler)
+ where TPixel : struct, IPixel
+ {
+ using (Image 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(TestImageProvider provider, string name, IResampler sampler)
+ where TPixel : struct, IPixel
+ {
+ using (Image 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(TestImageProvider provider, string name, IResampler sampler)
+ where TPixel : struct, IPixel
+ {
+ using (Image 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);
+ }
+ }
+}
\ No newline at end of file
diff --git a/tests/ImageSharp.Tests/Processors/Filters/PadTest.cs b/tests/ImageSharp.Tests/Processors/Filters/PadTest.cs
deleted file mode 100644
index 6095410a9..000000000
--- a/tests/ImageSharp.Tests/Processors/Filters/PadTest.cs
+++ /dev/null
@@ -1,31 +0,0 @@
-//
-// Copyright (c) James Jackson-South and contributors.
-// Licensed under the Apache License, Version 2.0.
-//
-
-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 image = file.CreateImage())
- using (FileStream output = File.OpenWrite($"{path}/{file.FileName}"))
- {
- image.Pad(image.Width + 50, image.Height + 50).Save(output);
- }
- }
- }
- }
-}
\ No newline at end of file
diff --git a/tests/ImageSharp.Tests/Processors/Filters/ResizeTests.cs b/tests/ImageSharp.Tests/Processors/Filters/ResizeTests.cs
deleted file mode 100644
index 8268c6b64..000000000
--- a/tests/ImageSharp.Tests/Processors/Filters/ResizeTests.cs
+++ /dev/null
@@ -1,350 +0,0 @@
-//
-// Copyright (c) James Jackson-South and contributors.
-// Licensed under the Apache License, Version 2.0.
-//
-namespace ImageSharp.Tests
-{
- using System;
- using System.IO;
-
- using ImageSharp.PixelFormats;
-
- using ImageSharp.Processing;
- using Xunit;
-
- public class ResizeTests : FileTestBase
- {
- public static readonly TheoryData ReSamplers =
- new TheoryData
- {
- { "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 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 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 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 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 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 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 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 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 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 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 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);
- }
- }
-}
\ No newline at end of file
diff --git a/tests/ImageSharp.Tests/TestUtilities/MeasureFixture.cs b/tests/ImageSharp.Tests/TestUtilities/MeasureFixture.cs
index 1dadc4884..1a6bf5d8b 100644
--- a/tests/ImageSharp.Tests/TestUtilities/MeasureFixture.cs
+++ b/tests/ImageSharp.Tests/TestUtilities/MeasureFixture.cs
@@ -1,4 +1,9 @@
-namespace ImageSharp.Tests
+//
+// Copyright (c) James Jackson-South and contributors.
+// Licensed under the Apache License, Version 2.0.
+//
+
+namespace ImageSharp.Tests
{
using System;
using System.Diagnostics;
@@ -24,8 +29,12 @@
/// The name of the operation to print to the output
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");
+ }
}
///