diff --git a/tests/ImageSharp.Tests/Processing/Binarization/DitherTests.cs b/tests/ImageSharp.Tests/Processing/Binarization/BinaryDitherTest.cs similarity index 86% rename from tests/ImageSharp.Tests/Processing/Binarization/DitherTests.cs rename to tests/ImageSharp.Tests/Processing/Binarization/BinaryDitherTest.cs index f801b2031..003f998d8 100644 --- a/tests/ImageSharp.Tests/Processing/Binarization/DitherTests.cs +++ b/tests/ImageSharp.Tests/Processing/Binarization/BinaryDitherTest.cs @@ -8,19 +8,19 @@ using Xunit; namespace SixLabors.ImageSharp.Tests.Processing.Binarization { - public class DitherTest : BaseImageOperationsExtensionTest + public class BinaryDitherTest : BaseImageOperationsExtensionTest { private readonly IOrderedDither orderedDither; private readonly IErrorDiffuser errorDiffuser; - public DitherTest() + public BinaryDitherTest() { this.orderedDither = KnownDitherers.BayerDither4x4; this.errorDiffuser = KnownDiffusers.FloydSteinberg; } [Fact] - public void Dither_CorrectProcessor() + public void BinaryDither_CorrectProcessor() { this.operations.BinaryDither(this.orderedDither); BinaryOrderedDitherProcessor p = this.Verify>(); @@ -30,7 +30,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Binarization } [Fact] - public void Dither_rect_CorrectProcessor() + public void BinaryDither_rect_CorrectProcessor() { this.operations.BinaryDither(this.orderedDither, this.rect); BinaryOrderedDitherProcessor p = this.Verify>(this.rect); @@ -39,7 +39,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Binarization Assert.Equal(NamedColors.Black, p.LowerColor); } [Fact] - public void Dither_index_CorrectProcessor() + public void BinaryDither_index_CorrectProcessor() { this.operations.BinaryDither(this.orderedDither, NamedColors.Yellow, NamedColors.HotPink); BinaryOrderedDitherProcessor p = this.Verify>(); @@ -49,7 +49,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Binarization } [Fact] - public void Dither_index_rect_CorrectProcessor() + public void BinaryDither_index_rect_CorrectProcessor() { this.operations.BinaryDither(this.orderedDither, NamedColors.Yellow, NamedColors.HotPink, this.rect); BinaryOrderedDitherProcessor p = this.Verify>(this.rect); @@ -59,7 +59,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Binarization [Fact] - public void Dither_ErrorDiffuser_CorrectProcessor() + public void BinaryDither_ErrorDiffuser_CorrectProcessor() { this.operations.BinaryDiffuse(this.errorDiffuser, .4F); BinaryErrorDiffusionProcessor p = this.Verify>(); @@ -70,7 +70,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Binarization } [Fact] - public void Dither_ErrorDiffuser_rect_CorrectProcessor() + public void BinaryDither_ErrorDiffuser_rect_CorrectProcessor() { this.operations.BinaryDiffuse(this.errorDiffuser, .3F, this.rect); BinaryErrorDiffusionProcessor p = this.Verify>(this.rect); @@ -81,7 +81,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Binarization } [Fact] - public void Dither_ErrorDiffuser_CorrectProcessorWithColors() + public void BinaryDither_ErrorDiffuser_CorrectProcessorWithColors() { this.operations.BinaryDiffuse(this.errorDiffuser, .5F, NamedColors.HotPink, NamedColors.Yellow); BinaryErrorDiffusionProcessor p = this.Verify>(); @@ -92,7 +92,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Binarization } [Fact] - public void Dither_ErrorDiffuser_rect_CorrectProcessorWithColors() + public void BinaryDither_ErrorDiffuser_rect_CorrectProcessorWithColors() { this.operations.BinaryDiffuse(this.errorDiffuser, .5F, NamedColors.HotPink, NamedColors.Yellow, this.rect); BinaryErrorDiffusionProcessor p = this.Verify>(this.rect); diff --git a/tests/ImageSharp.Tests/Processing/Dithering/DitherTest.cs b/tests/ImageSharp.Tests/Processing/Dithering/DitherTest.cs new file mode 100644 index 000000000..03ae17848 --- /dev/null +++ b/tests/ImageSharp.Tests/Processing/Dithering/DitherTest.cs @@ -0,0 +1,104 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using SixLabors.ImageSharp.Dithering; +using SixLabors.ImageSharp.PixelFormats; +using SixLabors.ImageSharp.Processing.Processors; +using Xunit; + +namespace SixLabors.ImageSharp.Tests.Processing.Binarization +{ + public class DitherTest : BaseImageOperationsExtensionTest + { + private readonly IOrderedDither orderedDither; + private readonly IErrorDiffuser errorDiffuser; + private readonly Rgba32[] TestPalette = + { + Rgba32.Red, + Rgba32.Green, + Rgba32.Blue + }; + + public DitherTest() + { + this.orderedDither = KnownDitherers.BayerDither4x4; + this.errorDiffuser = KnownDiffusers.FloydSteinberg; + } + + [Fact] + public void Dither_CorrectProcessor() + { + this.operations.Dither(this.orderedDither); + OrderedDitherPaletteProcessor p = this.Verify>(); + Assert.Equal(this.orderedDither, p.Dither); + Assert.Equal(NamedColors.WebSafePalette, p.Palette); + } + + [Fact] + public void Dither_rect_CorrectProcessor() + { + this.operations.Dither(this.orderedDither, this.rect); + OrderedDitherPaletteProcessor p = this.Verify>(this.rect); + Assert.Equal(this.orderedDither, p.Dither); + Assert.Equal(NamedColors.WebSafePalette, p.Palette); + } + [Fact] + public void Dither_index_CorrectProcessor() + { + this.operations.Dither(this.orderedDither, this.TestPalette); + OrderedDitherPaletteProcessor p = this.Verify>(); + Assert.Equal(this.orderedDither, p.Dither); + Assert.Equal(this.TestPalette, p.Palette); + } + + [Fact] + public void Dither_index_rect_CorrectProcessor() + { + this.operations.Dither(this.orderedDither, this.TestPalette, this.rect); + OrderedDitherPaletteProcessor p = this.Verify>(this.rect); + Assert.Equal(this.orderedDither, p.Dither); + Assert.Equal(this.TestPalette, p.Palette); + } + + + [Fact] + public void Dither_ErrorDiffuser_CorrectProcessor() + { + this.operations.Diffuse(this.errorDiffuser, .4F); + ErrorDiffusionPaletteProcessor p = this.Verify>(); + Assert.Equal(this.errorDiffuser, p.Diffuser); + Assert.Equal(.4F, p.Threshold); + Assert.Equal(NamedColors.WebSafePalette, p.Palette); + } + + [Fact] + public void Dither_ErrorDiffuser_rect_CorrectProcessor() + { + this.operations.Diffuse(this.errorDiffuser, .3F, this.rect); + ErrorDiffusionPaletteProcessor p = this.Verify>(this.rect); + Assert.Equal(this.errorDiffuser, p.Diffuser); + Assert.Equal(.3F, p.Threshold); + Assert.Equal(NamedColors.WebSafePalette, p.Palette); + } + + [Fact] + public void Dither_ErrorDiffuser_CorrectProcessorWithColors() + { + this.operations.Diffuse(this.errorDiffuser, .5F, this.TestPalette); + ErrorDiffusionPaletteProcessor p = this.Verify>(); + Assert.Equal(this.errorDiffuser, p.Diffuser); + Assert.Equal(.5F, p.Threshold); + Assert.Equal(this.TestPalette, p.Palette); + } + + [Fact] + public void Dither_ErrorDiffuser_rect_CorrectProcessorWithColors() + { + this.operations.Diffuse(this.errorDiffuser, .5F, this.TestPalette, this.rect); + ErrorDiffusionPaletteProcessor p = this.Verify>(this.rect); + Assert.Equal(this.errorDiffuser, p.Diffuser); + Assert.Equal(.5F, p.Threshold); + Assert.Equal(this.TestPalette, p.Palette); + } + } +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/Processing/Processors/Binarization/DitherTests.cs b/tests/ImageSharp.Tests/Processing/Processors/Binarization/BinaryDitherTests.cs similarity index 94% rename from tests/ImageSharp.Tests/Processing/Processors/Binarization/DitherTests.cs rename to tests/ImageSharp.Tests/Processing/Processors/Binarization/BinaryDitherTests.cs index 6db1434c6..aec201239 100644 --- a/tests/ImageSharp.Tests/Processing/Processors/Binarization/DitherTests.cs +++ b/tests/ImageSharp.Tests/Processing/Processors/Binarization/BinaryDitherTests.cs @@ -11,7 +11,7 @@ using Xunit; namespace SixLabors.ImageSharp.Tests.Processing.Processors.Binarization { - public class DitherTests : FileTestBase + public class BinaryDitherTests : FileTestBase { public static readonly string[] CommonTestImages = { @@ -47,7 +47,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Binarization [Theory] [WithFileCollection(nameof(CommonTestImages), nameof(Ditherers), DefaultPixelType)] [WithTestPatternImages(nameof(Ditherers), 100, 100, DefaultPixelType)] - public void DitherFilter_WorksWithAllDitherers(TestImageProvider provider, string name, IOrderedDither ditherer) + public void BinaryDitherFilter_WorksWithAllDitherers(TestImageProvider provider, string name, IOrderedDither ditherer) where TPixel : struct, IPixel { using (Image image = provider.GetImage()) @@ -72,7 +72,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Binarization [Theory] [WithFile(TestImages.Png.Bike, CommonNonDefaultPixelTypes)] - public void DitherFilter_ShouldNotDependOnSinglePixelType(TestImageProvider provider) + public void BinaryDitherFilter_ShouldNotDependOnSinglePixelType(TestImageProvider provider) where TPixel : struct, IPixel { using (Image image = provider.GetImage()) diff --git a/tests/ImageSharp.Tests/Processing/Processors/Dithering/DitherTests.cs b/tests/ImageSharp.Tests/Processing/Processors/Dithering/DitherTests.cs new file mode 100644 index 000000000..58c63d48c --- /dev/null +++ b/tests/ImageSharp.Tests/Processing/Processors/Dithering/DitherTests.cs @@ -0,0 +1,131 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using SixLabors.ImageSharp.Dithering; +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 +{ + public class DitherTests : FileTestBase + { + public static readonly string[] CommonTestImages = + { + TestImages.Png.CalliphoraPartial, TestImages.Png.Bike + }; + + public static readonly TheoryData Ditherers = new TheoryData + { + { "Bayer8x8", KnownDitherers.BayerDither8x8 }, + { "Bayer4x4", KnownDitherers.BayerDither4x4 }, + { "Ordered3x3", KnownDitherers.OrderedDither3x3 }, + { "Bayer2x2", KnownDitherers.BayerDither2x2 } + }; + + public static readonly TheoryData ErrorDiffusers = new TheoryData + { + { "Atkinson", KnownDiffusers.Atkinson }, + { "Burks", KnownDiffusers.Burks }, + { "FloydSteinberg", KnownDiffusers.FloydSteinberg }, + { "JarvisJudiceNinke", KnownDiffusers.JarvisJudiceNinke }, + { "Sierra2", KnownDiffusers.Sierra2 }, + { "Sierra3", KnownDiffusers.Sierra3 }, + { "SierraLite", KnownDiffusers.SierraLite }, + { "StevensonArce", KnownDiffusers.StevensonArce }, + { "Stucki", KnownDiffusers.Stucki }, + }; + + + private static IOrderedDither DefaultDitherer => KnownDitherers.BayerDither4x4; + + private static IErrorDiffuser DefaultErrorDiffuser => KnownDiffusers.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.Diffuse(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.Diffuse(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().VerifySimilarityIgnoreRegion(source, image, bounds); + } + } + + [Theory] + [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.Diffuse(DefaultErrorDiffuser, .5F, bounds)); + image.DebugSave(provider); + + ImageComparer.Tolerant().VerifySimilarityIgnoreRegion(source, image, bounds); + } + } + } +} \ No newline at end of file