// Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. using SixLabors.ImageSharp.Dithering; using SixLabors.ImageSharp.Dithering.Ordered; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison; using SixLabors.Primitives; using Xunit; // ReSharper disable InconsistentNaming namespace SixLabors.ImageSharp.Tests.Processing.Processors.Binarization { using System.Linq; public class DitherTests : FileTestBase { public static readonly string[] CommonTestImages = { TestImages.Png.CalliphoraPartial, TestImages.Png.Bike }; public static readonly TheoryData Ditherers = new TheoryData { { "Ordered", new Ordered() }, { "Bayer", new Bayer() } }; public static readonly TheoryData ErrorDiffusers = new TheoryData { { "Atkinson", new Atkinson() }, { "Burks", new Burks() }, { "FloydSteinberg", new FloydSteinberg() }, { "JarvisJudiceNinke", new JarvisJudiceNinke() }, { "Sierra2", new Sierra2() }, { "Sierra3", new Sierra3() }, { "SierraLite", new SierraLite() }, { "Stucki", new Stucki() }, }; private static IOrderedDither DefaultDitherer => new Ordered(); private static IErrorDiffuser DefaultErrorDiffuser => new Atkinson(); [Theory] [WithFileCollection(nameof(CommonTestImages), nameof(Ditherers), DefaultPixelType)] [WithTestPatternImages(nameof(Ditherers), 100, 100, DefaultPixelType)] public void DitherFilter_WorksWithAllDitherers(TestImageProvider provider, string name, IOrderedDither ditherer) where TPixel : struct, IPixel { using (Image image = provider.GetImage()) { image.Mutate(x => x.Dither(ditherer)); image.DebugSave(provider, name); } } [Theory] [WithFileCollection(nameof(CommonTestImages), nameof(ErrorDiffusers), DefaultPixelType)] [WithTestPatternImages(nameof(ErrorDiffusers), 100, 100, DefaultPixelType)] public void DiffusionFilter_WorksWithAllErrorDiffusers(TestImageProvider provider, string name, IErrorDiffuser diffuser) where TPixel : struct, IPixel { using (Image image = provider.GetImage()) { image.Mutate(x => x.Dither(diffuser, .5F)); image.DebugSave(provider, name); } } [Theory] [WithFile(TestImages.Png.Bike, CommonNonDefaultPixelTypes)] public void DitherFilter_ShouldNotDependOnSinglePixelType(TestImageProvider provider) where TPixel : struct, IPixel { using (Image image = provider.GetImage()) { image.Mutate(x => x.Dither(DefaultDitherer)); image.DebugSave(provider); } } [Theory] [WithFile(TestImages.Png.Bike, CommonNonDefaultPixelTypes)] public void DiffusionFilter_ShouldNotDependOnSinglePixelType(TestImageProvider provider) where TPixel : struct, IPixel { using (Image image = provider.GetImage()) { image.Mutate(x => x.Dither(DefaultErrorDiffuser, 0.5f)); image.DebugSave(provider); } } [Theory] [WithFile(TestImages.Png.CalliphoraPartial, DefaultPixelType)] public void ApplyDitherFilterInBox(TestImageProvider provider) where TPixel : struct, IPixel { using (Image source = provider.GetImage()) using (Image image = source.Clone()) { var bounds = new Rectangle(10, 10, image.Width / 2, image.Height / 2); image.Mutate(x => x.Dither(DefaultDitherer, bounds)); image.DebugSave(provider); ImageComparer.Tolerant().EnsureProcessorChangesAreConstrained(source, image, bounds); } } // TODO: Does not work because of a bug! Fix it! [Theory(Skip = "TODO: Does not work because of a bug! Fix it!")] [WithFile(TestImages.Png.CalliphoraPartial, DefaultPixelType)] public void ApplyDiffusionFilterInBox(TestImageProvider provider) where TPixel : struct, IPixel { using (Image source = provider.GetImage()) using (Image image = source.Clone()) { var bounds = new Rectangle(10, 10, image.Width / 2, image.Height / 2); image.Mutate(x => x.Dither(DefaultErrorDiffuser, .5F, bounds)); image.DebugSave(provider); ImageComparer.Tolerant().EnsureProcessorChangesAreConstrained(source, image, bounds); } } } }