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

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

@ -5,8 +5,6 @@
namespace ImageSharp.Tests namespace ImageSharp.Tests
{ {
using System.IO;
using ImageSharp.Dithering; using ImageSharp.Dithering;
using ImageSharp.Dithering.Ordered; using ImageSharp.Dithering.Ordered;
using ImageSharp.PixelFormats; using ImageSharp.PixelFormats;
@ -34,70 +32,66 @@ namespace ImageSharp.Tests
}; };
[Theory] [Theory]
[MemberData(nameof(Ditherers))] [WithFileCollection(nameof(AllBmpFiles), nameof(Ditherers), StandardPixelTypes)]
public void ImageShouldApplyDitherFilter(string name, IOrderedDither ditherer) public void ImageShouldApplyDitherFilter<TPixel>(TestImageProvider<TPixel> provider, string name, IOrderedDither ditherer)
where TPixel : struct, IPixel<TPixel>
{ {
string path = this.CreateOutputDirectory("Dither", "Dither"); using (Image<TPixel> image = provider.GetImage())
foreach (TestFile file in Files)
{ {
string filename = file.GetFileName(name); image.Dither(ditherer)
using (Image<Rgba32> image = file.CreateImage()) .DebugSave(provider, name, Extensions.Bmp);
using (FileStream output = File.OpenWrite($"{path}/{filename}"))
{
image.Dither(ditherer).Save(output);
}
} }
} }
[Theory] [Theory]
[MemberData(nameof(Ditherers))] [WithFileCollection(nameof(AllBmpFiles), nameof(Ditherers), StandardPixelTypes)]
public void ImageShouldApplyDitherFilterInBox(string name, IOrderedDither ditherer) public void ImageShouldApplyDitherFilterInBox<TPixel>(TestImageProvider<TPixel> provider, string name, IOrderedDither ditherer)
where TPixel : struct, IPixel<TPixel>
{ {
string path = this.CreateOutputDirectory("Dither", "Dither"); using (Image<TPixel> source = provider.GetImage())
using (var image = new Image<TPixel>(source))
foreach (TestFile file in Files)
{ {
string filename = file.GetFileName($"{name}-InBox"); var bounds = new Rectangle(10, 10, image.Width / 2, image.Height / 2);
using (Image<Rgba32> image = file.CreateImage())
using (FileStream output = File.OpenWrite($"{path}/{filename}")) image.Dither(ditherer, bounds)
{ .DebugSave(provider, name, Extensions.Bmp);
image.Dither(ditherer, new Rectangle(10, 10, image.Width / 2, image.Height / 2)).Save(output);
} // 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] [Theory]
[MemberData(nameof(ErrorDiffusers))] [WithFileCollection(nameof(AllBmpFiles), nameof(ErrorDiffusers), StandardPixelTypes)]
public void ImageShouldApplyDiffusionFilter(string name, IErrorDiffuser diffuser) public void ImageShouldApplyDiffusionFilter<TPixel>(TestImageProvider<TPixel> provider, string name, IErrorDiffuser diffuser)
where TPixel : struct, IPixel<TPixel>
{ {
string path = this.CreateOutputDirectory("Dither", "Diffusion"); using (Image<TPixel> image = provider.GetImage())
foreach (TestFile file in Files)
{ {
string filename = file.GetFileName(name); image.Dither(diffuser, .5F)
using (Image<Rgba32> image = file.CreateImage()) .DebugSave(provider, name, Extensions.Bmp);
using (FileStream output = File.OpenWrite($"{path}/{filename}"))
{
image.Dither(diffuser, .5F).Save(output);
}
} }
} }
[Theory] [Theory]
[MemberData(nameof(ErrorDiffusers))] [WithFileCollection(nameof(AllBmpFiles), nameof(ErrorDiffusers), StandardPixelTypes)]
public void ImageShouldApplyDiffusionFilterInBox(string name, IErrorDiffuser diffuser) public void ImageShouldApplyDiffusionFilterInBox<TPixel>(TestImageProvider<TPixel> provider, string name, IErrorDiffuser diffuser)
where TPixel : struct, IPixel<TPixel>
{ {
string path = this.CreateOutputDirectory("Dither", "Diffusion"); using (Image<TPixel> source = provider.GetImage())
using (var image = new Image<TPixel>(source))
foreach (TestFile file in Files)
{ {
string filename = file.GetFileName($"{name}-InBox"); var bounds = new Rectangle(10, 10, image.Width / 2, image.Height / 2);
using (Image<Rgba32> image = file.CreateImage())
using (FileStream output = File.OpenWrite($"{path}/{filename}")) image.Dither(diffuser,.5F, bounds)
{ .DebugSave(provider, name, Extensions.Bmp);
image.Dither(diffuser, .5F, new Rectangle(10, 10, image.Width / 2, image.Height / 2)).Save(output);
} // 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> /// </summary>
public class WithFileCollectionAttribute : ImageDataAttributeBase public class WithFileCollectionAttribute : ImageDataAttributeBase
{ {
private readonly string enumeratorMemberName; private readonly string fileEnumeratorMemberName;
/// <summary> /// <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 /// 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 /// <see cref="TestImageProvider{TPixel}"/> instances will be passed for each the pixel format defined by the pixelTypes parameter
/// </summary> /// </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="pixelTypes">The requested pixel types</param>
/// <param name="additionalParameters">Additional theory parameter values</param> /// <param name="additionalParameters">Additional theory parameter values</param>
public WithFileCollectionAttribute( public WithFileCollectionAttribute(
string enumeratorMemberName, string fileEnumeratorMemberName,
PixelTypes pixelTypes, PixelTypes pixelTypes,
params object[] additionalParameters) params object[] additionalParameters)
: base(null, pixelTypes, additionalParameters) : base(null, pixelTypes, additionalParameters)
{ {
this.enumeratorMemberName = enumeratorMemberName; this.fileEnumeratorMemberName = fileEnumeratorMemberName;
} }
/// <summary> /// <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 /// <see cref="TestImageProvider{TPixel}"/> instances will be passed for each the pixel format defined by the pixelTypes parameter
/// </summary> /// </summary>
/// <param name="enumeratorMemberName">The name of the static test class field/property enumerating the files</param> /// <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="pixelTypes">The requested pixel types</param>
/// <param name="additionalParameters">Additional theory parameter values</param> /// <param name="additionalParameters">Additional theory parameter values</param>
public WithFileCollectionAttribute( public WithFileCollectionAttribute(
@ -49,7 +49,7 @@ namespace ImageSharp.Tests
params object[] additionalParameters) params object[] additionalParameters)
: base(memberName, pixelTypes, additionalParameters) : base(memberName, pixelTypes, additionalParameters)
{ {
this.enumeratorMemberName = enumeratorMemberName; this.fileEnumeratorMemberName = enumeratorMemberName;
} }
/// <summary> /// <summary>
@ -67,6 +67,7 @@ namespace ImageSharp.Tests
return files.Select(f => new object[] { f }); return files.Select(f => new object[] { f });
} }
/// <inheritdoc/>
protected override string GetFactoryMethodName(MethodInfo testMethod) => "File"; protected override string GetFactoryMethodName(MethodInfo testMethod) => "File";
/// <summary> /// <summary>
@ -79,7 +80,7 @@ namespace ImageSharp.Tests
reflectionType != null; reflectionType != null;
reflectionType = reflectionType.GetTypeInfo().BaseType) reflectionType = reflectionType.GetTypeInfo().BaseType)
{ {
fieldInfo = reflectionType.GetRuntimeField(this.enumeratorMemberName); fieldInfo = reflectionType.GetRuntimeField(this.fileEnumeratorMemberName);
if (fieldInfo != null) if (fieldInfo != null)
{ {
break; break;
@ -104,7 +105,7 @@ namespace ImageSharp.Tests
reflectionType != null; reflectionType != null;
reflectionType = reflectionType.GetTypeInfo().BaseType) reflectionType = reflectionType.GetTypeInfo().BaseType)
{ {
propInfo = reflectionType.GetRuntimeProperty(this.enumeratorMemberName); propInfo = reflectionType.GetRuntimeProperty(this.fileEnumeratorMemberName);
if (propInfo != null) break; if (propInfo != null) break;
} }

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

@ -2,6 +2,7 @@
namespace ImageSharp.Tests namespace ImageSharp.Tests
{ {
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
@ -28,9 +29,16 @@ namespace ImageSharp.Tests
} }
else if (settings != null) 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) if (!bool.TryParse(Environment.GetEnvironmentVariable("CI"), out bool isCi) || !isCi)
{ {

Loading…
Cancel
Save