// --------------------------------------------------------------------------------------------------------------------
//
// Copyright (c) James South.
// Licensed under the Apache License, Version 2.0.
//
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.UnitTests
{
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using ImageProcessor.Imaging;
using ImageProcessor.Imaging.Filters.EdgeDetection;
using ImageProcessor.Imaging.Filters.Photo;
using NUnit.Framework;
///
/// Test harness for the image factory
///
[TestFixture]
public class ImageFactoryUnitTests
{
///
/// The list of images. Designed to speed up the tests a little.
///
private IEnumerable images;
///
/// Tests the loading of image from a file
///
[Test]
public void TestLoadImageFromFile()
{
foreach (FileInfo file in this.ListInputFiles())
{
using (ImageFactory imageFactory = new ImageFactory())
{
imageFactory.Load(file.FullName);
Assert.AreEqual(file.FullName, imageFactory.ImagePath);
Assert.IsNotNull(imageFactory.Image);
}
}
}
///
/// Tests the loading of image from a memory stream
///
[Test]
public void TestLoadImageFromMemory()
{
foreach (FileInfo file in this.ListInputFiles())
{
byte[] photoBytes = File.ReadAllBytes(file.FullName);
using (MemoryStream inStream = new MemoryStream(photoBytes))
{
using (ImageFactory imageFactory = new ImageFactory())
{
imageFactory.Load(inStream);
Assert.AreEqual(null, imageFactory.ImagePath);
Assert.IsNotNull(imageFactory.Image);
}
}
}
}
///
/// Tests that the save method actually saves a file
///
[Test]
public void TestSaveToDisk()
{
foreach (FileInfo file in this.ListInputFiles())
{
string outputFileName = string.Format("./output/{0}", file.Name);
using (ImageFactory imageFactory = new ImageFactory())
{
imageFactory.Load(file.FullName);
imageFactory.Save(outputFileName);
Assert.AreEqual(true, File.Exists(outputFileName));
}
}
}
///
/// Tests that the save method actually writes to memory
///
[Test]
public void TestSaveToMemory()
{
foreach (FileInfo file in this.ListInputFiles())
{
using (ImageFactory imageFactory = new ImageFactory())
{
imageFactory.Load(file.FullName);
using (MemoryStream s = new MemoryStream())
{
imageFactory.Save(s);
s.Seek(0, SeekOrigin.Begin);
Assert.AreEqual(true, s.Capacity > 0);
}
}
}
}
///
/// Tests that a filter is really applied by checking that the image is modified
///
[Test]
public void TestApplyEffectAlpha()
{
foreach (FileInfo file in this.ListInputFiles())
{
using (ImageFactory imageFactory = new ImageFactory())
{
imageFactory.Load(file.FullName);
Image original = (Image)imageFactory.Image.Clone();
imageFactory.Alpha(50);
Assert.AreNotEqual(original, imageFactory.Image);
}
}
}
///
/// Tests that brightness changes is really applied by checking that the image is modified
///
[Test]
public void TestApplyEffectBrightness()
{
foreach (FileInfo file in this.ListInputFiles())
{
using (ImageFactory imageFactory = new ImageFactory())
{
imageFactory.Load(file.FullName);
Image original = (Image)imageFactory.Image.Clone();
imageFactory.Brightness(50);
Assert.AreNotEqual(original, imageFactory.Image);
}
}
}
///
/// Tests that background color changes are really applied by checking that the image is modified
///
[Test]
public void TestApplyEffectBackgroundColor()
{
foreach (FileInfo file in this.ListInputFiles())
{
using (ImageFactory imageFactory = new ImageFactory())
{
imageFactory.Load(file.FullName);
Image original = (Image)imageFactory.Image.Clone();
imageFactory.BackgroundColor(Color.Yellow);
Assert.AreNotEqual(original, imageFactory.Image);
}
}
}
///
/// Tests that a contrast change is really applied by checking that the image is modified
///
[Test]
public void TestApplyEffectContrast()
{
foreach (FileInfo file in this.ListInputFiles())
{
using (ImageFactory imageFactory = new ImageFactory())
{
imageFactory.Load(file.FullName);
Image original = (Image)imageFactory.Image.Clone();
imageFactory.Contrast(50);
Assert.AreNotEqual(original, imageFactory.Image);
}
}
}
///
/// Tests that a saturation change is really applied by checking that the image is modified
///
[Test]
public void TestApplyEffectSaturation()
{
foreach (FileInfo file in this.ListInputFiles())
{
using (ImageFactory imageFactory = new ImageFactory())
{
imageFactory.Load(file.FullName);
Image original = (Image)imageFactory.Image.Clone();
imageFactory.Saturation(50);
Assert.AreNotEqual(original, imageFactory.Image);
}
}
}
///
/// Tests that a tint change is really applied by checking that the image is modified
///
[Test]
public void TestApplyEffectTint()
{
foreach (FileInfo file in this.ListInputFiles())
{
using (ImageFactory imageFactory = new ImageFactory())
{
imageFactory.Load(file.FullName);
Image original = (Image)imageFactory.Image.Clone();
imageFactory.Tint(Color.FromKnownColor(KnownColor.AliceBlue));
Assert.AreNotEqual(original, imageFactory.Image);
}
}
}
///
/// Tests that a vignette change is really applied by checking that the image is modified
///
[Test]
public void TestApplyEffectVignette()
{
foreach (FileInfo file in this.ListInputFiles())
{
using (ImageFactory imageFactory = new ImageFactory())
{
imageFactory.Load(file.FullName);
Image original = (Image)imageFactory.Image.Clone();
imageFactory.Vignette(Color.FromKnownColor(KnownColor.AliceBlue));
Assert.AreNotEqual(original, imageFactory.Image);
}
}
}
///
/// Tests that a filter is really applied by checking that the image is modified
///
[Test]
public void TestApplyEffectWatermark()
{
foreach (FileInfo file in this.ListInputFiles())
{
using (ImageFactory imageFactory = new ImageFactory())
{
imageFactory.Load(file.FullName);
Image original = (Image)imageFactory.Image.Clone();
imageFactory.Watermark(new TextLayer
{
FontFamily = new FontFamily("Arial"),
FontSize = 10,
Position = new Point(10, 10),
Text = "Lorem ipsum dolor"
});
Assert.AreNotEqual(original, imageFactory.Image);
}
}
}
///
/// Tests that a filter is really applied by checking that the image is modified
///
[Test]
public void TestApplyEffectBlur()
{
foreach (FileInfo file in this.ListInputFiles())
{
using (ImageFactory imageFactory = new ImageFactory())
{
imageFactory.Load(file.FullName);
Image original = (Image)imageFactory.Image.Clone();
imageFactory.GaussianBlur(5);
Assert.AreNotEqual(original, imageFactory.Image);
}
}
}
///
/// Tests that a filter is really applied by checking that the image is modified
///
[Test]
public void TestApplyEffectBlurWithLayer()
{
foreach (FileInfo file in this.ListInputFiles())
{
using (ImageFactory imageFactory = new ImageFactory())
{
imageFactory.Load(file.FullName);
Image original = (Image)imageFactory.Image.Clone();
imageFactory.GaussianBlur(new GaussianLayer { Sigma = 10, Size = 5, Threshold = 2 });
Assert.AreNotEqual(original, imageFactory.Image);
}
}
}
///
/// Tests that a filter is really applied by checking that the image is modified
///
[Test]
public void TestApplyEffectSharpen()
{
foreach (FileInfo file in this.ListInputFiles())
{
using (ImageFactory imageFactory = new ImageFactory())
{
imageFactory.Load(file.FullName);
Image original = (Image)imageFactory.Image.Clone();
imageFactory.GaussianSharpen(5);
Assert.AreNotEqual(original, imageFactory.Image);
}
}
}
///
/// Tests that a filter is really applied by checking that the image is modified
///
[Test]
public void TestApplyEffectSharpenWithLayer()
{
foreach (FileInfo file in this.ListInputFiles())
{
using (ImageFactory imageFactory = new ImageFactory())
{
imageFactory.Load(file.FullName);
Image original = (Image)imageFactory.Image.Clone();
imageFactory.GaussianSharpen(new GaussianLayer { Sigma = 10, Size = 5, Threshold = 2 });
Assert.AreNotEqual(original, imageFactory.Image);
}
}
}
///
/// Tests that all filters can be applied
///
[Test]
public void TestApplyEffectFilter()
{
foreach (FileInfo file in this.ListInputFiles())
{
using (ImageFactory imageFactory = new ImageFactory())
{
imageFactory.Load(file.FullName);
Image original = (Image)imageFactory.Image.Clone();
imageFactory.Filter(MatrixFilters.BlackWhite);
Assert.AreNotEqual(original, imageFactory.Image);
imageFactory.Reset();
imageFactory.Filter(MatrixFilters.Comic);
Assert.AreNotEqual(original, imageFactory.Image);
imageFactory.Reset();
imageFactory.Filter(MatrixFilters.Gotham);
Assert.AreNotEqual(original, imageFactory.Image);
imageFactory.Reset();
imageFactory.Filter(MatrixFilters.GreyScale);
Assert.AreNotEqual(original, imageFactory.Image);
imageFactory.Reset();
imageFactory.Filter(MatrixFilters.HiSatch);
Assert.AreNotEqual(original, imageFactory.Image);
imageFactory.Reset();
imageFactory.Filter(MatrixFilters.Invert);
Assert.AreNotEqual(original, imageFactory.Image);
imageFactory.Reset();
imageFactory.Filter(MatrixFilters.Lomograph);
Assert.AreNotEqual(original, imageFactory.Image);
imageFactory.Reset();
imageFactory.Filter(MatrixFilters.LoSatch);
Assert.AreNotEqual(original, imageFactory.Image);
imageFactory.Reset();
imageFactory.Filter(MatrixFilters.Polaroid);
Assert.AreNotEqual(original, imageFactory.Image);
imageFactory.Reset();
imageFactory.Filter(MatrixFilters.Sepia);
Assert.AreNotEqual(original, imageFactory.Image);
imageFactory.Reset();
}
}
}
///
/// Tests that a filter is really applied by checking that the image is modified
///
[Test]
public void TestRoundedCorners()
{
foreach (FileInfo file in this.ListInputFiles())
{
using (ImageFactory imageFactory = new ImageFactory())
{
imageFactory.Load(file.FullName);
Image original = (Image)imageFactory.Image.Clone();
imageFactory.RoundedCorners(new RoundedCornerLayer(5));
Assert.AreNotEqual(original, imageFactory.Image);
}
}
}
///
/// Tests that the image is well resized using constraints
///
[Test]
public void TestResizeConstraints()
{
const int MaxSize = 200;
foreach (FileInfo file in this.ListInputFiles())
{
using (ImageFactory imageFactory = new ImageFactory())
{
imageFactory.Load(file.FullName);
imageFactory.Constrain(new Size(MaxSize, MaxSize));
Assert.LessOrEqual(imageFactory.Image.Width, MaxSize);
Assert.LessOrEqual(imageFactory.Image.Height, MaxSize);
}
}
}
///
/// Tests that the image is well cropped
///
[Test]
public void TestCrop()
{
const int MaxSize = 20;
foreach (FileInfo file in this.ListInputFiles())
{
using (ImageFactory imageFactory = new ImageFactory())
{
imageFactory.Load(file.FullName);
Image original = (Image)imageFactory.Image.Clone();
imageFactory.Crop(new Rectangle(0, 0, MaxSize, MaxSize));
Assert.AreNotEqual(original, imageFactory.Image);
Assert.AreEqual(MaxSize, imageFactory.Image.Width);
Assert.LessOrEqual(MaxSize, imageFactory.Image.Height);
}
}
}
///
/// Tests that the image is well cropped
///
[Test]
public void TestCropWithCropLayer()
{
const int MaxSize = 20;
foreach (FileInfo file in this.ListInputFiles())
{
using (ImageFactory imageFactory = new ImageFactory())
{
imageFactory.Load(file.FullName);
Image original = (Image)imageFactory.Image.Clone();
imageFactory.Crop(new CropLayer(0, 0, MaxSize, MaxSize, CropMode.Pixels));
Assert.AreNotEqual(original, imageFactory.Image);
Assert.AreEqual(MaxSize, imageFactory.Image.Width);
Assert.LessOrEqual(MaxSize, imageFactory.Image.Height);
}
}
}
///
/// Tests that the image is flipped
///
[Test]
public void TestFlip()
{
foreach (FileInfo file in this.ListInputFiles())
{
using (ImageFactory imageFactory = new ImageFactory())
{
imageFactory.Load(file.FullName);
Image original = (Image)imageFactory.Image.Clone();
imageFactory.Flip(true);
Assert.AreNotEqual(original, imageFactory.Image);
Assert.AreEqual(original.Width, imageFactory.Image.Width);
Assert.AreEqual(original.Height, imageFactory.Image.Height);
imageFactory.Reset();
imageFactory.Flip();
Assert.AreNotEqual(original, imageFactory.Image);
Assert.AreEqual(original.Width, imageFactory.Image.Width);
Assert.AreEqual(original.Height, imageFactory.Image.Height);
}
}
}
///
/// Tests that the image is resized
///
[Test]
public void TestResize()
{
const int NewSize = 150;
foreach (FileInfo file in this.ListInputFiles())
{
using (ImageFactory imageFactory = new ImageFactory())
{
imageFactory.Load(file.FullName);
imageFactory.Resize(new Size(NewSize, NewSize));
Assert.AreEqual(NewSize, imageFactory.Image.Width);
Assert.AreEqual(NewSize, imageFactory.Image.Height);
}
}
}
///
/// Tests that the image is resized
///
[Test]
public void TestResizeWithLayer()
{
const int NewSize = 150;
foreach (FileInfo file in this.ListInputFiles())
{
using (ImageFactory imageFactory = new ImageFactory())
{
imageFactory.Load(file.FullName);
imageFactory.Resize(new ResizeLayer(new Size(NewSize, NewSize), ResizeMode.Stretch, AnchorPosition.Left));
Assert.AreEqual(NewSize, imageFactory.Image.Width);
Assert.AreEqual(NewSize, imageFactory.Image.Height);
}
}
}
///
/// Tests that the image is resized
///
[Test]
public void TestRotate()
{
foreach (FileInfo file in this.ListInputFiles())
{
using (ImageFactory imageFactory = new ImageFactory())
{
imageFactory.Load(file.FullName);
Image original = (Image)imageFactory.Image.Clone();
imageFactory.Rotate(90);
Assert.AreEqual(original.Height, imageFactory.Image.Width);
Assert.AreEqual(original.Width, imageFactory.Image.Height);
}
}
}
///
/// Tests that the images hue has been altered.
///
[Test]
public void TestHue()
{
foreach (FileInfo file in this.ListInputFiles())
{
using (ImageFactory imageFactory = new ImageFactory())
{
imageFactory.Load(file.FullName);
Image original = (Image)imageFactory.Image.Clone();
imageFactory.Hue(90);
Assert.AreNotEqual(original, imageFactory.Image);
imageFactory.Reset();
imageFactory.Hue(116, true);
Assert.AreNotEqual(original, imageFactory.Image);
}
}
}
///
/// Tests that the image has been pixelated.
///
[Test]
public void TestPixelate()
{
foreach (FileInfo file in this.ListInputFiles())
{
using (ImageFactory imageFactory = new ImageFactory())
{
imageFactory.Load(file.FullName);
Image original = (Image)imageFactory.Image.Clone();
imageFactory.Pixelate(8);
Assert.AreNotEqual(original, imageFactory.Image);
}
}
}
///
/// Tests that the images quality has been set.
///
[Test]
public void TestQuality()
{
foreach (FileInfo file in this.ListInputFiles())
{
using (ImageFactory imageFactory = new ImageFactory())
{
imageFactory.Load(file.FullName);
int original = imageFactory.CurrentImageFormat.Quality;
imageFactory.Quality(69);
int updated = imageFactory.CurrentImageFormat.Quality;
Assert.AreNotEqual(original, updated);
}
}
}
///
/// Tests that the image has had a color replaced.
///
[Test]
public void TestReplaceColor()
{
foreach (FileInfo file in this.ListInputFiles())
{
using (ImageFactory imageFactory = new ImageFactory())
{
imageFactory.Load(file.FullName);
Image original = (Image)imageFactory.Image.Clone();
imageFactory.ReplaceColor(Color.White, Color.Black, 90);
Assert.AreNotEqual(original, imageFactory.Image);
}
}
}
///
/// Tests that the various edge detection algorithms are applied.
///
[Test]
public void TestEdgeDetection()
{
foreach (FileInfo file in this.ListInputFiles())
{
using (ImageFactory imageFactory = new ImageFactory())
{
imageFactory.Load(file.FullName);
Image original = (Image)imageFactory.Image.Clone();
imageFactory.DetectEdges(new KayyaliEdgeFilter());
Assert.AreNotEqual(original, imageFactory.Image);
imageFactory.Reset();
imageFactory.DetectEdges(new KirschEdgeFilter());
Assert.AreNotEqual(original, imageFactory.Image);
imageFactory.Reset();
imageFactory.DetectEdges(new Laplacian3X3EdgeFilter());
Assert.AreNotEqual(original, imageFactory.Image);
imageFactory.Reset();
imageFactory.DetectEdges(new Laplacian5X5EdgeFilter());
Assert.AreNotEqual(original, imageFactory.Image);
imageFactory.Reset();
imageFactory.DetectEdges(new LaplacianOfGaussianEdgeFilter());
Assert.AreNotEqual(original, imageFactory.Image);
imageFactory.Reset();
imageFactory.DetectEdges(new PrewittEdgeFilter());
Assert.AreNotEqual(original, imageFactory.Image);
imageFactory.Reset();
imageFactory.DetectEdges(new RobertsCrossEdgeFilter());
Assert.AreNotEqual(original, imageFactory.Image);
imageFactory.Reset();
imageFactory.DetectEdges(new ScharrEdgeFilter());
Assert.AreNotEqual(original, imageFactory.Image);
imageFactory.Reset();
imageFactory.DetectEdges(new SobelEdgeFilter());
Assert.AreNotEqual(original, imageFactory.Image);
imageFactory.Reset();
}
}
}
///
/// Gets the files matching the given extensions.
///
///
/// The .
///
///
/// The extensions.
///
///
/// A collection of
///
///
/// The extensions variable is null.
///
private static IEnumerable GetFilesByExtensions(DirectoryInfo dir, params string[] extensions)
{
if (extensions == null)
{
throw new ArgumentNullException("extensions");
}
IEnumerable files = dir.EnumerateFiles();
return files.Where(f => extensions.Contains(f.Extension, StringComparer.OrdinalIgnoreCase));
}
///
/// Lists the input files in the Images folder
///
/// The list of files.
private IEnumerable ListInputFiles()
{
if (this.images != null)
{
return this.images;
}
DirectoryInfo directoryInfo = new DirectoryInfo("./Images");
this.images = GetFilesByExtensions(directoryInfo, new[] { ".jpg", ".jpeg", ".png", ".gif", ".tiff", ".bmp", ".webp" });
return this.images;
}
}
}