Browse Source

improve DitherTests CQ

pull/902/head
Anton Firszov 7 years ago
parent
commit
226b5f53dd
  1. 133
      tests/ImageSharp.Tests/Processing/Processors/Dithering/DitherTests.cs

133
tests/ImageSharp.Tests/Processing/Processors/Dithering/DitherTests.cs

@ -2,128 +2,131 @@
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison;
using SixLabors.ImageSharp.Processing; using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Processing.Dithering; using SixLabors.ImageSharp.Processing.Dithering;
using SixLabors.ImageSharp.Processing.Processors.Dithering; using SixLabors.ImageSharp.Processing.Processors.Dithering;
using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison;
using SixLabors.Primitives; using SixLabors.Primitives;
using Xunit; using Xunit;
namespace SixLabors.ImageSharp.Tests.Processing.Processors.Binarization namespace SixLabors.ImageSharp.Tests.Processing.Processors.Binarization
{ {
public class DitherTests : FileTestBase public class DitherTests
{ {
public static readonly string[] CommonTestImages = public const PixelTypes CommonNonDefaultPixelTypes =
{ PixelTypes.Rgba32 | PixelTypes.Bgra32 | PixelTypes.Rgb24 | PixelTypes.RgbaVector;
TestImages.Png.CalliphoraPartial, TestImages.Png.Bike
}; public static readonly string[] CommonTestImages = { TestImages.Png.CalliphoraPartial, TestImages.Png.Bike };
public static readonly TheoryData<string, IOrderedDither> OrderedDitherers = new TheoryData<string, IOrderedDither> public static readonly TheoryData<IErrorDiffuser> ErrorDiffusers = new TheoryData<IErrorDiffuser>
{ {
{ "Bayer8x8", KnownDitherers.BayerDither8x8 }, KnownDiffusers.Atkinson,
{ "Bayer4x4", KnownDitherers.BayerDither4x4 }, KnownDiffusers.Burks,
{ "Ordered3x3", KnownDitherers.OrderedDither3x3 }, KnownDiffusers.FloydSteinberg,
{ "Bayer2x2", KnownDitherers.BayerDither2x2 } KnownDiffusers.JarvisJudiceNinke,
}; KnownDiffusers.Sierra2,
KnownDiffusers.Sierra3,
public static readonly TheoryData<string, IErrorDiffuser> ErrorDiffusers = new TheoryData<string, IErrorDiffuser> KnownDiffusers.SierraLite,
{ KnownDiffusers.StevensonArce,
{ "Atkinson", KnownDiffusers.Atkinson }, KnownDiffusers.Stucki,
{ "Burks", KnownDiffusers.Burks }, };
{ "FloydSteinberg", KnownDiffusers.FloydSteinberg },
{ "JarvisJudiceNinke", KnownDiffusers.JarvisJudiceNinke }, public static readonly TheoryData<IOrderedDither> OrderedDitherers = new TheoryData<IOrderedDither>
{ "Sierra2", KnownDiffusers.Sierra2 }, {
{ "Sierra3", KnownDiffusers.Sierra3 }, KnownDitherers.BayerDither8x8,
{ "SierraLite", KnownDiffusers.SierraLite }, KnownDitherers.BayerDither4x4,
{ "StevensonArce", KnownDiffusers.StevensonArce }, KnownDitherers.OrderedDither3x3,
{ "Stucki", KnownDiffusers.Stucki }, KnownDitherers.BayerDither2x2
}; };
private static IOrderedDither DefaultDitherer => KnownDitherers.BayerDither4x4; private static IOrderedDither DefaultDitherer => KnownDitherers.BayerDither4x4;
private static IErrorDiffuser DefaultErrorDiffuser => KnownDiffusers.Atkinson; private static IErrorDiffuser DefaultErrorDiffuser => KnownDiffusers.Atkinson;
[Theory] [Theory]
[WithFileCollection(nameof(CommonTestImages), nameof(OrderedDitherers), DefaultPixelType)] [WithFile(TestImages.Png.CalliphoraPartial, PixelTypes.Rgba32)]
[WithTestPatternImages(nameof(OrderedDitherers), 100, 100, DefaultPixelType)] public void ApplyDiffusionFilterInBox<TPixel>(TestImageProvider<TPixel> provider)
public void DitherFilter_WorksWithAllDitherers<TPixel>(TestImageProvider<TPixel> provider, string name, IOrderedDither ditherer)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using (Image<TPixel> source = provider.GetImage())
using (Image<TPixel> image = source.Clone())
{ {
image.Mutate(x => x.Dither(ditherer)); var bounds = new Rectangle(10, 10, image.Width / 2, image.Height / 2);
image.DebugSave(provider, name);
image.Mutate(x => x.Diffuse(DefaultErrorDiffuser, .5F, bounds));
image.DebugSave(provider);
ImageComparer.Tolerant().VerifySimilarityIgnoreRegion(source, image, bounds);
} }
} }
[Theory] [Theory]
[WithFileCollection(nameof(CommonTestImages), nameof(ErrorDiffusers), DefaultPixelType)] [WithFile(TestImages.Png.CalliphoraPartial, PixelTypes.Rgba32)]
[WithTestPatternImages(nameof(ErrorDiffusers), 100, 100, DefaultPixelType)] public void ApplyDitherFilterInBox<TPixel>(TestImageProvider<TPixel> provider)
public void DiffusionFilter_WorksWithAllErrorDiffusers<TPixel>(TestImageProvider<TPixel> provider, string name, IErrorDiffuser diffuser)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using (Image<TPixel> source = provider.GetImage())
using (Image<TPixel> image = source.Clone())
{ {
image.Mutate(x => x.Diffuse(diffuser, .5F)); var bounds = new Rectangle(10, 10, image.Width / 2, image.Height / 2);
image.DebugSave(provider, name);
image.Mutate(x => x.Dither(DefaultDitherer, bounds));
image.DebugSave(provider);
ImageComparer.Tolerant().VerifySimilarityIgnoreRegion(source, image, bounds);
} }
} }
[Theory] [Theory]
[WithFile(TestImages.Png.Filter0, CommonNonDefaultPixelTypes)] [WithFile(TestImages.Png.Filter0, CommonNonDefaultPixelTypes)]
public void DitherFilter_ShouldNotDependOnSinglePixelType<TPixel>(TestImageProvider<TPixel> provider) public void DiffusionFilter_ShouldNotDependOnSinglePixelType<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using (Image<TPixel> image = provider.GetImage())
{ {
image.Mutate(x => x.Dither(DefaultDitherer)); image.Mutate(x => x.Diffuse(DefaultErrorDiffuser, 0.5f));
image.DebugSave(provider); image.DebugSave(provider);
} }
} }
[Theory] [Theory]
[WithFile(TestImages.Png.Filter0, CommonNonDefaultPixelTypes)] [WithFileCollection(nameof(CommonTestImages), nameof(ErrorDiffusers), PixelTypes.Rgba32)]
public void DiffusionFilter_ShouldNotDependOnSinglePixelType<TPixel>(TestImageProvider<TPixel> provider) public void DiffusionFilter_WorksWithAllErrorDiffusers<TPixel>(
TestImageProvider<TPixel> provider,
IErrorDiffuser diffuser)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using (Image<TPixel> image = provider.GetImage())
{ {
image.Mutate(x => x.Diffuse(DefaultErrorDiffuser, 0.5f)); image.Mutate(x => x.Diffuse(diffuser, .5F));
image.DebugSave(provider); image.DebugSave(provider, diffuser.GetType().Name);
} }
} }
[Theory] [Theory]
[WithFile(TestImages.Png.CalliphoraPartial, DefaultPixelType)] [WithFile(TestImages.Png.Filter0, CommonNonDefaultPixelTypes)]
public void ApplyDitherFilterInBox<TPixel>(TestImageProvider<TPixel> provider) public void DitherFilter_ShouldNotDependOnSinglePixelType<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> source = provider.GetImage()) using (Image<TPixel> image = provider.GetImage())
using (Image<TPixel> image = source.Clone())
{ {
var bounds = new Rectangle(10, 10, image.Width / 2, image.Height / 2); image.Mutate(x => x.Dither(DefaultDitherer));
image.Mutate(x => x.Dither(DefaultDitherer, bounds));
image.DebugSave(provider); image.DebugSave(provider);
ImageComparer.Tolerant().VerifySimilarityIgnoreRegion(source, image, bounds);
} }
} }
[Theory] [Theory]
[WithFile(TestImages.Png.CalliphoraPartial, DefaultPixelType)] [WithFileCollection(nameof(CommonTestImages), nameof(OrderedDitherers), PixelTypes.Rgba32)]
public void ApplyDiffusionFilterInBox<TPixel>(TestImageProvider<TPixel> provider) public void DitherFilter_WorksWithAllDitherers<TPixel>(
TestImageProvider<TPixel> provider,
IOrderedDither ditherer)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> source = provider.GetImage()) using (Image<TPixel> image = provider.GetImage())
using (Image<TPixel> image = source.Clone())
{ {
var bounds = new Rectangle(10, 10, image.Width / 2, image.Height / 2); image.Mutate(x => x.Dither(ditherer));
image.DebugSave(provider, ditherer.GetType().Name);
image.Mutate(x => x.Diffuse(DefaultErrorDiffuser, .5F, bounds));
image.DebugSave(provider);
ImageComparer.Tolerant().VerifySimilarityIgnoreRegion(source, image, bounds);
} }
} }
} }

Loading…
Cancel
Save