// -------------------------------------------------------------------------------------------------------------------- // // Copyright (c) James South. // Licensed under the Apache License, Version 2.0. // // // Unit tests for the ImageFactory (loading of images) // // -------------------------------------------------------------------------------------------------------------------- namespace ImageProcessor.UnitTests { using System; using System.IO; using System.Collections.Generic; using NUnit.Framework; /// /// Test harness for the image factory /// [TestFixture] public class ImageFactoryUnitTests { /// /// Lists the input files in the Images folder /// /// The list of files. private static IEnumerable ListInputFiles() { return Directory.GetFiles("./Images"); } /// /// Tests the loading of image from a file /// [Test] public void TestLoadImageFromFile() { foreach (var fileName in ListInputFiles()) { using (var imageFactory = new ImageFactory()) { imageFactory.Load(fileName); Assert.AreEqual(fileName, imageFactory.ImagePath); Assert.IsNotNull(imageFactory.Image); } } } /// > /// Tests the loading of image from a memory stream /// [Test] public void TestLoadImageFromMemory() { foreach (var fileName in ListInputFiles()) { byte[] photoBytes = File.ReadAllBytes(fileName); using (var inStream = new MemoryStream(photoBytes)) { using (var imageFactory = new ImageFactory()) { imageFactory.Load(inStream); Assert.AreEqual(null, imageFactory.ImagePath); Assert.IsNotNull(imageFactory.Image); } } } } /// /// Tests that a filter is really applied by checking that the image is modified /// [Test] public void TestApplyEffectAlpha() { foreach (var fileName in ListInputFiles()) { using (var imageFactory = new ImageFactory()) { imageFactory.Load(fileName); var original = imageFactory.Image.Clone(); imageFactory.Alpha(50); Assert.AreNotEqual(original, imageFactory.Image); } } } /// /// Tests that a filter is really applied by checking that the image is modified /// [Test] public void TestApplyEffectBrightness() { foreach (var fileName in ListInputFiles()) { using (var imageFactory = new ImageFactory()) { imageFactory.Load(fileName); var original = imageFactory.Image.Clone(); imageFactory.Brightness(50); Assert.AreNotEqual(original, imageFactory.Image); } } } /// /// Tests that a filter is really applied by checking that the image is modified /// [Test] public void TestApplyEffectContrast() { foreach (var fileName in ListInputFiles()) { using (var imageFactory = new ImageFactory()) { imageFactory.Load(fileName); var original = imageFactory.Image.Clone(); imageFactory.Contrast(50); Assert.AreNotEqual(original, imageFactory.Image); } } } /// /// Tests that a filter is really applied by checking that the image is modified /// [Test] public void TestApplyEffectBlur() { foreach (var fileName in ListInputFiles()) { using (var imageFactory = new ImageFactory()) { imageFactory.Load(fileName); var original = 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 (var fileName in ListInputFiles()) { using (var imageFactory = new ImageFactory()) { imageFactory.Load(fileName); var original = imageFactory.Image.Clone(); imageFactory.GaussianBlur(new Imaging.GaussianLayer() { Sigma = 10, Size = 5, Threshold = 2 }); Assert.AreNotEqual(original, imageFactory.Image); } } } /// /// Tests that all filters can be applied /// [Test] public void TestApplyEffectFilter() { foreach (var fileName in ListInputFiles()) { using (var imageFactory = new ImageFactory()) { imageFactory.Load(fileName); var original = imageFactory.Image.Clone(); imageFactory.Filter(Imaging.Filters.MatrixFilters.BlackWhite); Assert.AreNotEqual(original, imageFactory.Image); imageFactory.Reset(); imageFactory.Filter(Imaging.Filters.MatrixFilters.Comic); Assert.AreNotEqual(original, imageFactory.Image); imageFactory.Reset(); imageFactory.Filter(Imaging.Filters.MatrixFilters.Gotham); Assert.AreNotEqual(original, imageFactory.Image); imageFactory.Reset(); imageFactory.Filter(Imaging.Filters.MatrixFilters.GreyScale); Assert.AreNotEqual(original, imageFactory.Image); imageFactory.Reset(); imageFactory.Filter(Imaging.Filters.MatrixFilters.HiSatch); Assert.AreNotEqual(original, imageFactory.Image); imageFactory.Reset(); imageFactory.Filter(Imaging.Filters.MatrixFilters.Invert); Assert.AreNotEqual(original, imageFactory.Image); imageFactory.Reset(); imageFactory.Filter(Imaging.Filters.MatrixFilters.Lomograph); Assert.AreNotEqual(original, imageFactory.Image); imageFactory.Reset(); imageFactory.Filter(Imaging.Filters.MatrixFilters.LoSatch); Assert.AreNotEqual(original, imageFactory.Image); imageFactory.Reset(); imageFactory.Filter(Imaging.Filters.MatrixFilters.Polaroid); Assert.AreNotEqual(original, imageFactory.Image); imageFactory.Reset(); imageFactory.Filter(Imaging.Filters.MatrixFilters.Sepia); Assert.AreNotEqual(original, imageFactory.Image); imageFactory.Reset(); } } } /// /// Tests that the image is well resized using constraints /// [Test] public void TestResizeConstraints() { const int maxSize = 200; foreach (var fileName in ListInputFiles()) { using (var imageFactory = new ImageFactory()) { imageFactory.Load(fileName); var original = imageFactory.Image.Clone(); imageFactory.Constrain(new System.Drawing.Size(maxSize, maxSize)); Assert.AreNotEqual(original, imageFactory.Image); 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 (var fileName in ListInputFiles()) { using (var imageFactory = new ImageFactory()) { imageFactory.Load(fileName); var original = imageFactory.Image.Clone(); imageFactory.Crop(new System.Drawing.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 (var fileName in ListInputFiles()) { using (var imageFactory = new ImageFactory()) { imageFactory.Load(fileName); var original = imageFactory.Image.Clone(); imageFactory.Crop(new Imaging.CropLayer(0, 0, maxSize, maxSize, Imaging.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 (var fileName in ListInputFiles()) { using (var imageFactory = new ImageFactory()) { imageFactory.Load(fileName); var original = (System.Drawing.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(false); Assert.AreNotEqual(original, imageFactory.Image); Assert.AreEqual(original.Width, imageFactory.Image.Width); Assert.AreEqual(original.Height, imageFactory.Image.Height); } } } } }