Browse Source

Finish dither tests

pull/232/head
James Jackson-South 9 years ago
parent
commit
cf06ff4d59
  1. 15
      tests/ImageSharp.Tests/Processing/Binarization/BinaryThresholdTest.cs
  2. 86
      tests/ImageSharp.Tests/Processing/Binarization/DitherTest.cs
  3. 17
      tests/ImageSharp.Tests/TestUtilities/Attributes/WithFileCollectionAttribute.cs
  4. 12
      tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs

15
tests/ImageSharp.Tests/Processing/Binarization/BinaryThresholdTest.cs

@ -11,20 +11,27 @@ namespace ImageSharp.Tests.Processing.Binarization
public class BinaryThresholdTest : FileTestBase
{
public static readonly TheoryData<float> BinaryThresholdValues
= new TheoryData<float>
{
.25F,
.75F
};
[Theory]
[WithFileCollection(nameof(AllBmpFiles), StandardPixelTypes, .75F)]
[WithFileCollection(nameof(AllBmpFiles), nameof(BinaryThresholdValues), StandardPixelTypes)]
public void ImageShouldApplyBinaryThresholdFilter<TPixel>(TestImageProvider<TPixel> provider, float value)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
image.BinaryThreshold(value)
.DebugSave(provider, null, Extensions.Bmp);
.DebugSave(provider, value, Extensions.Bmp);
}
}
[Theory]
[WithFileCollection(nameof(AllBmpFiles), StandardPixelTypes, .75F)]
[WithFileCollection(nameof(AllBmpFiles), nameof(BinaryThresholdValues), StandardPixelTypes)]
public void ImageShouldApplyBinaryThresholdInBox<TPixel>(TestImageProvider<TPixel> provider, float value)
where TPixel : struct, IPixel<TPixel>
{
@ -35,7 +42,7 @@ namespace ImageSharp.Tests.Processing.Binarization
var bounds = new Rectangle(10, 10, image.Width / 2, image.Height / 2);
image.BinaryThreshold(value, bounds)
.DebugSave(provider, null, Extensions.Bmp);
.DebugSave(provider, value, Extensions.Bmp);
// Draw identical shapes over the bounded and compare to ensure changes are constrained.
image.Fill(NamedColors<TPixel>.HotPink, bounds);

86
tests/ImageSharp.Tests/Processing/Binarization/DitherTest.cs

@ -5,8 +5,6 @@
namespace ImageSharp.Tests
{
using System.IO;
using ImageSharp.Dithering;
using ImageSharp.Dithering.Ordered;
using ImageSharp.PixelFormats;
@ -34,70 +32,66 @@ namespace ImageSharp.Tests
};
[Theory]
[MemberData(nameof(Ditherers))]
public void ImageShouldApplyDitherFilter(string name, IOrderedDither ditherer)
[WithFileCollection(nameof(AllBmpFiles), nameof(Ditherers), StandardPixelTypes)]
public void ImageShouldApplyDitherFilter<TPixel>(TestImageProvider<TPixel> provider, string name, IOrderedDither ditherer)
where TPixel : struct, IPixel<TPixel>
{
string path = this.CreateOutputDirectory("Dither", "Dither");
foreach (TestFile file in Files)
using (Image<TPixel> image = provider.GetImage())
{
string filename = file.GetFileName(name);
using (Image<Rgba32> image = file.CreateImage())
using (FileStream output = File.OpenWrite($"{path}/{filename}"))
{
image.Dither(ditherer).Save(output);
}
image.Dither(ditherer)
.DebugSave(provider, name, Extensions.Bmp);
}
}
[Theory]
[MemberData(nameof(Ditherers))]
public void ImageShouldApplyDitherFilterInBox(string name, IOrderedDither ditherer)
[WithFileCollection(nameof(AllBmpFiles), nameof(Ditherers), StandardPixelTypes)]
public void ImageShouldApplyDitherFilterInBox<TPixel>(TestImageProvider<TPixel> provider, string name, IOrderedDither ditherer)
where TPixel : struct, IPixel<TPixel>
{
string path = this.CreateOutputDirectory("Dither", "Dither");
foreach (TestFile file in Files)
using (Image<TPixel> source = provider.GetImage())
using (var image = new Image<TPixel>(source))
{
string filename = file.GetFileName($"{name}-InBox");
using (Image<Rgba32> image = file.CreateImage())
using (FileStream output = File.OpenWrite($"{path}/{filename}"))
{
image.Dither(ditherer, new Rectangle(10, 10, image.Width / 2, image.Height / 2)).Save(output);
}
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<TPixel>.HotPink, bounds);
source.Fill(NamedColors<TPixel>.HotPink, bounds);
ImageComparer.CheckSimilarity(image, source);
}
}
[Theory]
[MemberData(nameof(ErrorDiffusers))]
public void ImageShouldApplyDiffusionFilter(string name, IErrorDiffuser diffuser)
[WithFileCollection(nameof(AllBmpFiles), nameof(ErrorDiffusers), StandardPixelTypes)]
public void ImageShouldApplyDiffusionFilter<TPixel>(TestImageProvider<TPixel> provider, string name, IErrorDiffuser diffuser)
where TPixel : struct, IPixel<TPixel>
{
string path = this.CreateOutputDirectory("Dither", "Diffusion");
foreach (TestFile file in Files)
using (Image<TPixel> image = provider.GetImage())
{
string filename = file.GetFileName(name);
using (Image<Rgba32> image = file.CreateImage())
using (FileStream output = File.OpenWrite($"{path}/{filename}"))
{
image.Dither(diffuser, .5F).Save(output);
}
image.Dither(diffuser, .5F)
.DebugSave(provider, name, Extensions.Bmp);
}
}
[Theory]
[MemberData(nameof(ErrorDiffusers))]
public void ImageShouldApplyDiffusionFilterInBox(string name, IErrorDiffuser diffuser)
[WithFileCollection(nameof(AllBmpFiles), nameof(ErrorDiffusers), StandardPixelTypes)]
public void ImageShouldApplyDiffusionFilterInBox<TPixel>(TestImageProvider<TPixel> provider, string name, IErrorDiffuser diffuser)
where TPixel : struct, IPixel<TPixel>
{
string path = this.CreateOutputDirectory("Dither", "Diffusion");
foreach (TestFile file in Files)
using (Image<TPixel> source = provider.GetImage())
using (var image = new Image<TPixel>(source))
{
string filename = file.GetFileName($"{name}-InBox");
using (Image<Rgba32> image = file.CreateImage())
using (FileStream output = File.OpenWrite($"{path}/{filename}"))
{
image.Dither(diffuser, .5F, new Rectangle(10, 10, image.Width / 2, image.Height / 2)).Save(output);
}
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<TPixel>.HotPink, bounds);
source.Fill(NamedColors<TPixel>.HotPink, bounds);
ImageComparer.CheckSimilarity(image, source);
}
}
}

17
tests/ImageSharp.Tests/TestUtilities/Attributes/WithFileCollectionAttribute.cs

@ -16,22 +16,22 @@ namespace ImageSharp.Tests
/// </summary>
public class WithFileCollectionAttribute : ImageDataAttributeBase
{
private readonly string enumeratorMemberName;
private readonly string fileEnumeratorMemberName;
/// <summary>
/// Triggers passing <see cref="TestImageProvider{TPixel}"/> instances which read an image for each file being enumerated by the (static) test class field/property defined by enumeratorMemberName
/// <see cref="TestImageProvider{TPixel}"/> instances will be passed for each the pixel format defined by the pixelTypes parameter
/// </summary>
/// <param name="enumeratorMemberName">The name of the static test class field/property enumerating the files</param>
/// <param name="fileEnumeratorMemberName">The name of the static test class field/property enumerating the files</param>
/// <param name="pixelTypes">The requested pixel types</param>
/// <param name="additionalParameters">Additional theory parameter values</param>
public WithFileCollectionAttribute(
string enumeratorMemberName,
string fileEnumeratorMemberName,
PixelTypes pixelTypes,
params object[] additionalParameters)
: base(null, pixelTypes, additionalParameters)
{
this.enumeratorMemberName = enumeratorMemberName;
this.fileEnumeratorMemberName = fileEnumeratorMemberName;
}
/// <summary>
@ -39,7 +39,7 @@ namespace ImageSharp.Tests
/// <see cref="TestImageProvider{TPixel}"/> instances will be passed for each the pixel format defined by the pixelTypes parameter
/// </summary>
/// <param name="enumeratorMemberName">The name of the static test class field/property enumerating the files</param>
/// <param name="memberName">The member name</param>
/// <param name="memberName">The member name for enumerating method parameters</param>
/// <param name="pixelTypes">The requested pixel types</param>
/// <param name="additionalParameters">Additional theory parameter values</param>
public WithFileCollectionAttribute(
@ -49,7 +49,7 @@ namespace ImageSharp.Tests
params object[] additionalParameters)
: base(memberName, pixelTypes, additionalParameters)
{
this.enumeratorMemberName = enumeratorMemberName;
this.fileEnumeratorMemberName = enumeratorMemberName;
}
/// <summary>
@ -67,6 +67,7 @@ namespace ImageSharp.Tests
return files.Select(f => new object[] { f });
}
/// <inheritdoc/>
protected override string GetFactoryMethodName(MethodInfo testMethod) => "File";
/// <summary>
@ -79,7 +80,7 @@ namespace ImageSharp.Tests
reflectionType != null;
reflectionType = reflectionType.GetTypeInfo().BaseType)
{
fieldInfo = reflectionType.GetRuntimeField(this.enumeratorMemberName);
fieldInfo = reflectionType.GetRuntimeField(this.fileEnumeratorMemberName);
if (fieldInfo != null)
{
break;
@ -104,7 +105,7 @@ namespace ImageSharp.Tests
reflectionType != null;
reflectionType = reflectionType.GetTypeInfo().BaseType)
{
propInfo = reflectionType.GetRuntimeProperty(this.enumeratorMemberName);
propInfo = reflectionType.GetRuntimeProperty(this.fileEnumeratorMemberName);
if (propInfo != null) break;
}

12
tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs

@ -2,6 +2,7 @@
namespace ImageSharp.Tests
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
@ -28,9 +29,16 @@ namespace ImageSharp.Tests
}
else if (settings != null)
{
System.Collections.Generic.IEnumerable<PropertyInfo> properties = settings.GetType().GetRuntimeProperties();
if (settings.GetType().GetTypeInfo().IsPrimitive)
{
tag = settings.ToString();
}
else
{
IEnumerable<PropertyInfo> properties = settings.GetType().GetRuntimeProperties();
tag = string.Join("_", properties.ToDictionary(x => x.Name, x => x.GetValue(settings)).Select(x => $"{x.Key}-{x.Value}"));
tag = string.Join("_", properties.ToDictionary(x => x.Name, x => x.GetValue(settings)).Select(x => $"{x.Key}-{x.Value}"));
}
}
if (!bool.TryParse(Environment.GetEnvironmentVariable("CI"), out bool isCi) || !isCi)
{

Loading…
Cancel
Save