|
|
|
@ -1,4 +1,4 @@ |
|
|
|
// <copyright file="SamplerTests.cs" company="James Jackson-South">
|
|
|
|
// <copyright file="ResizeTests.cs" company="James Jackson-South">
|
|
|
|
// Copyright (c) James Jackson-South and contributors.
|
|
|
|
// Licensed under the Apache License, Version 2.0.
|
|
|
|
// </copyright>
|
|
|
|
@ -9,8 +9,10 @@ namespace ImageProcessorCore.Tests |
|
|
|
|
|
|
|
using Xunit; |
|
|
|
|
|
|
|
public class SamplerTests : FileTestBase |
|
|
|
public class ResizeTests : FileTestBase |
|
|
|
{ |
|
|
|
private const string path = "TestOutput/Resize"; |
|
|
|
|
|
|
|
public static readonly TheoryData<string, IResampler> ReSamplers = |
|
|
|
new TheoryData<string, IResampler> |
|
|
|
{ |
|
|
|
@ -31,46 +33,13 @@ namespace ImageProcessorCore.Tests |
|
|
|
//{ "Welch", new WelchResampler() }
|
|
|
|
}; |
|
|
|
|
|
|
|
public static readonly TheoryData<RotateType, FlipType> RotateFlips = new TheoryData<RotateType, FlipType> |
|
|
|
{ |
|
|
|
{ RotateType.None, FlipType.Vertical }, |
|
|
|
{ RotateType.None, FlipType.Horizontal }, |
|
|
|
{ RotateType.Rotate90, FlipType.None }, |
|
|
|
{ RotateType.Rotate180, FlipType.None }, |
|
|
|
{ RotateType.Rotate270, FlipType.None }, |
|
|
|
}; |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void ImageShouldPad() |
|
|
|
{ |
|
|
|
if (!Directory.Exists("TestOutput/Pad")) |
|
|
|
{ |
|
|
|
Directory.CreateDirectory("TestOutput/Pad"); |
|
|
|
} |
|
|
|
|
|
|
|
foreach (string file in Files) |
|
|
|
{ |
|
|
|
using (FileStream stream = File.OpenRead(file)) |
|
|
|
{ |
|
|
|
string filename = Path.GetFileName(file); |
|
|
|
|
|
|
|
Image image = new Image(stream); |
|
|
|
using (FileStream output = File.OpenWrite($"TestOutput/Pad/{filename}")) |
|
|
|
{ |
|
|
|
image.Pad(image.Width + 50, image.Height + 50, this.ProgressUpdate) |
|
|
|
.Save(output); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
[Theory] |
|
|
|
[MemberData("ReSamplers")] |
|
|
|
public void ImageShouldResize(string name, IResampler sampler) |
|
|
|
{ |
|
|
|
if (!Directory.Exists("TestOutput/Resize")) |
|
|
|
if (!Directory.Exists(path)) |
|
|
|
{ |
|
|
|
Directory.CreateDirectory("TestOutput/Resize"); |
|
|
|
Directory.CreateDirectory(path); |
|
|
|
} |
|
|
|
|
|
|
|
foreach (string file in Files) |
|
|
|
@ -80,7 +49,7 @@ namespace ImageProcessorCore.Tests |
|
|
|
string filename = Path.GetFileNameWithoutExtension(file) + "-" + name + Path.GetExtension(file); |
|
|
|
|
|
|
|
Image image = new Image(stream); |
|
|
|
using (FileStream output = File.OpenWrite($"TestOutput/Resize/{filename}")) |
|
|
|
using (FileStream output = File.OpenWrite($"{path}/{filename}")) |
|
|
|
{ |
|
|
|
image.Resize(image.Width / 2, image.Height / 2, sampler, false, this.ProgressUpdate) |
|
|
|
.Save(output); |
|
|
|
@ -92,13 +61,13 @@ namespace ImageProcessorCore.Tests |
|
|
|
[Fact] |
|
|
|
public void ImageShouldResizeWidthAndKeepAspect() |
|
|
|
{ |
|
|
|
if (!Directory.Exists("TestOutput/Resize")) |
|
|
|
const string name = "FixedWidth"; |
|
|
|
|
|
|
|
if (!Directory.Exists(path)) |
|
|
|
{ |
|
|
|
Directory.CreateDirectory("TestOutput/Resize"); |
|
|
|
Directory.CreateDirectory(path); |
|
|
|
} |
|
|
|
|
|
|
|
var name = "FixedWidth"; |
|
|
|
|
|
|
|
foreach (string file in Files) |
|
|
|
{ |
|
|
|
using (FileStream stream = File.OpenRead(file)) |
|
|
|
@ -106,7 +75,7 @@ namespace ImageProcessorCore.Tests |
|
|
|
string filename = Path.GetFileNameWithoutExtension(file) + "-" + name + Path.GetExtension(file); |
|
|
|
|
|
|
|
Image image = new Image(stream); |
|
|
|
using (FileStream output = File.OpenWrite($"TestOutput/Resize/{filename}")) |
|
|
|
using (FileStream output = File.OpenWrite($"{path}/{filename}")) |
|
|
|
{ |
|
|
|
image.Resize(image.Width / 3, 0, new TriangleResampler(), false, this.ProgressUpdate) |
|
|
|
.Save(output); |
|
|
|
@ -118,13 +87,13 @@ namespace ImageProcessorCore.Tests |
|
|
|
[Fact] |
|
|
|
public void ImageShouldResizeHeightAndKeepAspect() |
|
|
|
{ |
|
|
|
if (!Directory.Exists("TestOutput/Resize")) |
|
|
|
const string name = "FixedHeight"; |
|
|
|
|
|
|
|
if (!Directory.Exists(path)) |
|
|
|
{ |
|
|
|
Directory.CreateDirectory("TestOutput/Resize"); |
|
|
|
Directory.CreateDirectory(path); |
|
|
|
} |
|
|
|
|
|
|
|
string name = "FixedHeight"; |
|
|
|
|
|
|
|
foreach (string file in Files) |
|
|
|
{ |
|
|
|
using (FileStream stream = File.OpenRead(file)) |
|
|
|
@ -132,7 +101,7 @@ namespace ImageProcessorCore.Tests |
|
|
|
string filename = Path.GetFileNameWithoutExtension(file) + "-" + name + Path.GetExtension(file); |
|
|
|
|
|
|
|
Image image = new Image(stream); |
|
|
|
using (FileStream output = File.OpenWrite($"TestOutput/Resize/{filename}")) |
|
|
|
using (FileStream output = File.OpenWrite($"{path}/{filename}")) |
|
|
|
{ |
|
|
|
image.Resize(0, image.Height / 3, new TriangleResampler(), false, this.ProgressUpdate) |
|
|
|
.Save(output); |
|
|
|
@ -144,19 +113,21 @@ namespace ImageProcessorCore.Tests |
|
|
|
[Fact] |
|
|
|
public void ImageShouldResizeWithCropMode() |
|
|
|
{ |
|
|
|
if (!Directory.Exists("TestOutput/ResizeCrop")) |
|
|
|
const string name = "Crop"; |
|
|
|
|
|
|
|
if (!Directory.Exists(path)) |
|
|
|
{ |
|
|
|
Directory.CreateDirectory("TestOutput/ResizeCrop"); |
|
|
|
Directory.CreateDirectory(path); |
|
|
|
} |
|
|
|
|
|
|
|
foreach (string file in Files) |
|
|
|
{ |
|
|
|
using (FileStream stream = File.OpenRead(file)) |
|
|
|
{ |
|
|
|
string filename = Path.GetFileName(file); |
|
|
|
string filename = Path.GetFileNameWithoutExtension(file) + "-" + name + Path.GetExtension(file); |
|
|
|
|
|
|
|
Image image = new Image(stream); |
|
|
|
using (FileStream output = File.OpenWrite($"TestOutput/ResizeCrop/{filename}")) |
|
|
|
using (FileStream output = File.OpenWrite($"{path}/{filename}")) |
|
|
|
{ |
|
|
|
ResizeOptions options = new ResizeOptions() |
|
|
|
{ |
|
|
|
@ -173,19 +144,21 @@ namespace ImageProcessorCore.Tests |
|
|
|
[Fact] |
|
|
|
public void ImageShouldResizeWithPadMode() |
|
|
|
{ |
|
|
|
if (!Directory.Exists("TestOutput/ResizePad")) |
|
|
|
const string name = "Pad"; |
|
|
|
|
|
|
|
if (!Directory.Exists(path)) |
|
|
|
{ |
|
|
|
Directory.CreateDirectory("TestOutput/ResizePad"); |
|
|
|
Directory.CreateDirectory(path); |
|
|
|
} |
|
|
|
|
|
|
|
foreach (string file in Files) |
|
|
|
{ |
|
|
|
using (FileStream stream = File.OpenRead(file)) |
|
|
|
{ |
|
|
|
string filename = Path.GetFileName(file); |
|
|
|
string filename = Path.GetFileNameWithoutExtension(file) + "-" + name + Path.GetExtension(file); |
|
|
|
|
|
|
|
Image image = new Image(stream); |
|
|
|
using (FileStream output = File.OpenWrite($"TestOutput/ResizePad/{filename}")) |
|
|
|
using (FileStream output = File.OpenWrite($"{path}/{filename}")) |
|
|
|
{ |
|
|
|
ResizeOptions options = new ResizeOptions() |
|
|
|
{ |
|
|
|
@ -203,19 +176,21 @@ namespace ImageProcessorCore.Tests |
|
|
|
[Fact] |
|
|
|
public void ImageShouldResizeWithBoxPadMode() |
|
|
|
{ |
|
|
|
if (!Directory.Exists("TestOutput/ResizeBoxPad")) |
|
|
|
const string name = "BoxPad"; |
|
|
|
|
|
|
|
if (!Directory.Exists(path)) |
|
|
|
{ |
|
|
|
Directory.CreateDirectory("TestOutput/ResizeBoxPad"); |
|
|
|
Directory.CreateDirectory(path); |
|
|
|
} |
|
|
|
|
|
|
|
foreach (string file in Files) |
|
|
|
{ |
|
|
|
using (FileStream stream = File.OpenRead(file)) |
|
|
|
{ |
|
|
|
string filename = Path.GetFileName(file); |
|
|
|
string filename = Path.GetFileNameWithoutExtension(file) + "-" + name + Path.GetExtension(file); |
|
|
|
|
|
|
|
Image image = new Image(stream); |
|
|
|
using (FileStream output = File.OpenWrite($"TestOutput/ResizeBoxPad/{filename}")) |
|
|
|
using (FileStream output = File.OpenWrite($"{path}/{filename}")) |
|
|
|
{ |
|
|
|
ResizeOptions options = new ResizeOptions() |
|
|
|
{ |
|
|
|
@ -233,25 +208,26 @@ namespace ImageProcessorCore.Tests |
|
|
|
[Fact] |
|
|
|
public void ImageShouldResizeWithMaxMode() |
|
|
|
{ |
|
|
|
if (!Directory.Exists("TestOutput/ResizeMax")) |
|
|
|
const string name = "Max"; |
|
|
|
|
|
|
|
if (!Directory.Exists(path)) |
|
|
|
{ |
|
|
|
Directory.CreateDirectory("TestOutput/ResizeMax"); |
|
|
|
Directory.CreateDirectory(path); |
|
|
|
} |
|
|
|
|
|
|
|
foreach (string file in Files) |
|
|
|
{ |
|
|
|
using (FileStream stream = File.OpenRead(file)) |
|
|
|
{ |
|
|
|
string filename = Path.GetFileName(file); |
|
|
|
string filename = Path.GetFileNameWithoutExtension(file) + "-" + name + Path.GetExtension(file); |
|
|
|
|
|
|
|
Image image = new Image(stream); |
|
|
|
using (FileStream output = File.OpenWrite($"TestOutput/ResizeMax/{filename}")) |
|
|
|
using (FileStream output = File.OpenWrite($"{path}/{filename}")) |
|
|
|
{ |
|
|
|
ResizeOptions options = new ResizeOptions() |
|
|
|
{ |
|
|
|
Size = new Size(300, 300), |
|
|
|
Mode = ResizeMode.Max, |
|
|
|
//Sampler = new NearestNeighborResampler()
|
|
|
|
Mode = ResizeMode.Max |
|
|
|
}; |
|
|
|
|
|
|
|
image.Resize(options, this.ProgressUpdate) |
|
|
|
@ -264,19 +240,21 @@ namespace ImageProcessorCore.Tests |
|
|
|
[Fact] |
|
|
|
public void ImageShouldResizeWithMinMode() |
|
|
|
{ |
|
|
|
if (!Directory.Exists("TestOutput/ResizeMin")) |
|
|
|
const string name = "Min"; |
|
|
|
|
|
|
|
if (!Directory.Exists(path)) |
|
|
|
{ |
|
|
|
Directory.CreateDirectory("TestOutput/ResizeMin"); |
|
|
|
Directory.CreateDirectory(path); |
|
|
|
} |
|
|
|
|
|
|
|
foreach (string file in Files) |
|
|
|
{ |
|
|
|
using (FileStream stream = File.OpenRead(file)) |
|
|
|
{ |
|
|
|
string filename = Path.GetFileName(file); |
|
|
|
string filename = Path.GetFileNameWithoutExtension(file) + "-" + name + Path.GetExtension(file); |
|
|
|
|
|
|
|
Image image = new Image(stream); |
|
|
|
using (FileStream output = File.OpenWrite($"TestOutput/ResizeMin/{filename}")) |
|
|
|
using (FileStream output = File.OpenWrite($"{path}/{filename}")) |
|
|
|
{ |
|
|
|
ResizeOptions options = new ResizeOptions() |
|
|
|
{ |
|
|
|
@ -294,19 +272,21 @@ namespace ImageProcessorCore.Tests |
|
|
|
[Fact] |
|
|
|
public void ImageShouldResizeWithStretchMode() |
|
|
|
{ |
|
|
|
if (!Directory.Exists("TestOutput/ResizeStretch")) |
|
|
|
const string name = "Stretch"; |
|
|
|
|
|
|
|
if (!Directory.Exists(path)) |
|
|
|
{ |
|
|
|
Directory.CreateDirectory("TestOutput/ResizeStretch"); |
|
|
|
Directory.CreateDirectory(path); |
|
|
|
} |
|
|
|
|
|
|
|
foreach (string file in Files) |
|
|
|
{ |
|
|
|
using (FileStream stream = File.OpenRead(file)) |
|
|
|
{ |
|
|
|
string filename = Path.GetFileName(file); |
|
|
|
string filename = Path.GetFileNameWithoutExtension(file) + "-" + name + Path.GetExtension(file); |
|
|
|
|
|
|
|
Image image = new Image(stream); |
|
|
|
using (FileStream output = File.OpenWrite($"TestOutput/ResizeStretch/{filename}")) |
|
|
|
using (FileStream output = File.OpenWrite($"{path}/{filename}")) |
|
|
|
{ |
|
|
|
ResizeOptions options = new ResizeOptions() |
|
|
|
{ |
|
|
|
@ -321,158 +301,6 @@ namespace ImageProcessorCore.Tests |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//[Fact]
|
|
|
|
//public void ImageShouldNotDispose()
|
|
|
|
//{
|
|
|
|
// if (!Directory.Exists("TestOutput/Dispose"))
|
|
|
|
// {
|
|
|
|
// Directory.CreateDirectory("TestOutput/Dispose");
|
|
|
|
// }
|
|
|
|
|
|
|
|
// foreach (string file in Files)
|
|
|
|
// {
|
|
|
|
// using (FileStream stream = File.OpenRead(file))
|
|
|
|
// {
|
|
|
|
// string filename = Path.GetFileName(file);
|
|
|
|
|
|
|
|
// Image image = new Image(stream);
|
|
|
|
// image = image.BackgroundColor(Color.RebeccaPurple);
|
|
|
|
// using (FileStream output = File.OpenWrite($"TestOutput/Dispose/{filename}"))
|
|
|
|
// {
|
|
|
|
// ResizeOptions options = new ResizeOptions()
|
|
|
|
// {
|
|
|
|
// Size = new Size(image.Width - 10, image.Height),
|
|
|
|
// Mode = ResizeMode.Stretch
|
|
|
|
// };
|
|
|
|
|
|
|
|
// image.Resize(options, this.ProgressUpdate)
|
|
|
|
// .Save(output);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
|
|
|
|
[Theory] |
|
|
|
[MemberData("RotateFlips")] |
|
|
|
public void ImageShouldRotateFlip(RotateType rotateType, FlipType flipType) |
|
|
|
{ |
|
|
|
if (!Directory.Exists("TestOutput/RotateFlip")) |
|
|
|
{ |
|
|
|
Directory.CreateDirectory("TestOutput/RotateFlip"); |
|
|
|
} |
|
|
|
|
|
|
|
foreach (string file in Files) |
|
|
|
{ |
|
|
|
using (FileStream stream = File.OpenRead(file)) |
|
|
|
{ |
|
|
|
string filename = Path.GetFileNameWithoutExtension(file) + "-" + rotateType + flipType + Path.GetExtension(file); |
|
|
|
|
|
|
|
Image image = new Image(stream); |
|
|
|
using (FileStream output = File.OpenWrite($"TestOutput/RotateFlip/{filename}")) |
|
|
|
{ |
|
|
|
image.RotateFlip(rotateType, flipType, this.ProgressUpdate) |
|
|
|
.Save(output); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void ImageShouldRotate() |
|
|
|
{ |
|
|
|
if (!Directory.Exists("TestOutput/Rotate")) |
|
|
|
{ |
|
|
|
Directory.CreateDirectory("TestOutput/Rotate"); |
|
|
|
} |
|
|
|
|
|
|
|
foreach (string file in Files) |
|
|
|
{ |
|
|
|
using (FileStream stream = File.OpenRead(file)) |
|
|
|
{ |
|
|
|
string filename = Path.GetFileName(file); |
|
|
|
|
|
|
|
Image image = new Image(stream); |
|
|
|
using (FileStream output = File.OpenWrite($"TestOutput/Rotate/{filename}")) |
|
|
|
{ |
|
|
|
image.Rotate(-170, this.ProgressUpdate) |
|
|
|
.Save(output); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void ImageShouldSkew() |
|
|
|
{ |
|
|
|
if (!Directory.Exists("TestOutput/Skew")) |
|
|
|
{ |
|
|
|
Directory.CreateDirectory("TestOutput/Skew"); |
|
|
|
} |
|
|
|
|
|
|
|
// Matches live example http://www.w3schools.com/css/tryit.asp?filename=trycss3_transform_skew
|
|
|
|
foreach (string file in Files) |
|
|
|
{ |
|
|
|
using (FileStream stream = File.OpenRead(file)) |
|
|
|
{ |
|
|
|
string filename = Path.GetFileName(file); |
|
|
|
|
|
|
|
Image image = new Image(stream); |
|
|
|
using (FileStream output = File.OpenWrite($"TestOutput/Skew/{filename}")) |
|
|
|
{ |
|
|
|
image.Skew(-20, -10, this.ProgressUpdate) |
|
|
|
.Save(output); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void ImageShouldEntropyCrop() |
|
|
|
{ |
|
|
|
if (!Directory.Exists("TestOutput/EntropyCrop")) |
|
|
|
{ |
|
|
|
Directory.CreateDirectory("TestOutput/EntropyCrop"); |
|
|
|
} |
|
|
|
|
|
|
|
foreach (string file in Files) |
|
|
|
{ |
|
|
|
using (FileStream stream = File.OpenRead(file)) |
|
|
|
{ |
|
|
|
string filename = Path.GetFileNameWithoutExtension(file) + "-EntropyCrop" + Path.GetExtension(file); |
|
|
|
|
|
|
|
Image image = new Image(stream); |
|
|
|
using (FileStream output = File.OpenWrite($"TestOutput/EntropyCrop/{filename}")) |
|
|
|
{ |
|
|
|
image.EntropyCrop(.5f, this.ProgressUpdate).Save(output); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void ImageShouldCrop() |
|
|
|
{ |
|
|
|
if (!Directory.Exists("TestOutput/Crop")) |
|
|
|
{ |
|
|
|
Directory.CreateDirectory("TestOutput/Crop"); |
|
|
|
} |
|
|
|
|
|
|
|
foreach (string file in Files) |
|
|
|
{ |
|
|
|
using (FileStream stream = File.OpenRead(file)) |
|
|
|
{ |
|
|
|
|
|
|
|
string filename = Path.GetFileNameWithoutExtension(file) + "-Crop" + Path.GetExtension(file); |
|
|
|
|
|
|
|
Image image = new Image(stream); |
|
|
|
using (FileStream output = File.OpenWrite($"TestOutput/Crop/{filename}")) |
|
|
|
{ |
|
|
|
image.Crop(image.Width / 2, image.Height / 2, this.ProgressUpdate).Save(output); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
[Theory] |
|
|
|
[InlineData(-2, 0)] |
|
|
|
[InlineData(-1, 0)] |