// // Copyright (c) James Jackson-South and contributors. // Licensed under the Apache License, Version 2.0. // namespace ImageSharp.Tests { using ImageSharp.Dithering; using ImageSharp.Dithering.Ordered; using ImageSharp.PixelFormats; using Xunit; public class DitherTest : FileTestBase { 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() }, }; [Theory] [WithFileCollection(nameof(AllBmpFiles), nameof(Ditherers), StandardPixelTypes)] public void ImageShouldApplyDitherFilter(TestImageProvider provider, string name, IOrderedDither ditherer) where TPixel : struct, IPixel { using (Image image = provider.GetImage()) { image.Dither(ditherer) .DebugSave(provider, name, Extensions.Bmp); } } [Theory] [WithFileCollection(nameof(AllBmpFiles), nameof(Ditherers), StandardPixelTypes)] public void ImageShouldApplyDitherFilterInBox(TestImageProvider provider, string name, IOrderedDither ditherer) where TPixel : struct, IPixel { using (Image source = provider.GetImage()) using (var image = new Image(source)) { var bounds = new Rectangle(10, 10, image.Width / 2, image.Height / 2); image.Dither(ditherer, bounds) .DebugSave(provider, name, Extensions.Bmp); // Draw identical shapes over the bounded and compare to ensure changes are constrained. image.Fill(NamedColors.HotPink, bounds); source.Fill(NamedColors.HotPink, bounds); ImageComparer.CheckSimilarity(image, source); } } [Theory] [WithFileCollection(nameof(AllBmpFiles), nameof(ErrorDiffusers), StandardPixelTypes)] public void ImageShouldApplyDiffusionFilter(TestImageProvider provider, string name, IErrorDiffuser diffuser) where TPixel : struct, IPixel { using (Image image = provider.GetImage()) { image.Dither(diffuser, .5F) .DebugSave(provider, name, Extensions.Bmp); } } [Theory] [WithFileCollection(nameof(AllBmpFiles), nameof(ErrorDiffusers), StandardPixelTypes)] public void ImageShouldApplyDiffusionFilterInBox(TestImageProvider provider, string name, IErrorDiffuser diffuser) where TPixel : struct, IPixel { using (Image source = provider.GetImage()) using (var image = new Image(source)) { var bounds = new Rectangle(10, 10, image.Width / 2, image.Height / 2); image.Dither(diffuser,.5F, bounds) .DebugSave(provider, name, Extensions.Bmp); // Draw identical shapes over the bounded and compare to ensure changes are constrained. image.Fill(NamedColors.HotPink, bounds); source.Fill(NamedColors.HotPink, bounds); ImageComparer.CheckSimilarity(image, source); } } } }