Browse Source

PixelTypes bugfix + polishing ResizeTests

af/merge-core
Anton Firszov 9 years ago
parent
commit
8548e5d680
  1. 5
      tests/ImageSharp.Tests/FileTestBase.cs
  2. 29
      tests/ImageSharp.Tests/Processing/Processors/Convolution/DetectEdgesTest.cs
  3. 41
      tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeTests.cs
  4. 0
      tests/ImageSharp.Tests/TestUtilities/ImageComparison/Exceptions/ImageDimensionsMismatchException.cs
  5. 0
      tests/ImageSharp.Tests/TestUtilities/ImageComparison/Exceptions/ImagePixelsAreDifferentException.cs
  6. 0
      tests/ImageSharp.Tests/TestUtilities/ImageComparison/Exceptions/ImagesSimilarityException.cs
  7. 8
      tests/ImageSharp.Tests/TestUtilities/PixelTypes.cs
  8. 19
      tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensions.cs
  9. 27
      tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs

5
tests/ImageSharp.Tests/FileTestBase.cs

@ -49,6 +49,11 @@ namespace ImageSharp.Tests
/// </summary> /// </summary>
public const PixelTypes DefaultPixelType = PixelTypes.Rgba32; public const PixelTypes DefaultPixelType = PixelTypes.Rgba32;
/// <summary>
/// A few other pixel types to prove that a processor is not bound to a single one.
/// </summary>
public const PixelTypes CommonNonDefaultPixelTypes = PixelTypes.Rgba32 | PixelTypes.Bgra32 | PixelTypes.RgbaVector;
public static class Extensions public static class Extensions
{ {
public const string Bmp = "bmp"; public const string Bmp = "bmp";

29
tests/ImageSharp.Tests/Processing/Processors/Convolution/DetectEdgesTest.cs

@ -3,6 +3,7 @@
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
// </copyright> // </copyright>
// ReSharper disable InconsistentNaming
namespace ImageSharp.Tests.Processing.Processors.Convolution namespace ImageSharp.Tests.Processing.Processors.Convolution
{ {
using ImageSharp.PixelFormats; using ImageSharp.PixelFormats;
@ -34,7 +35,7 @@ namespace ImageSharp.Tests.Processing.Processors.Convolution
[Theory] [Theory]
[WithTestPatternImages(nameof(DetectEdgesFilters), 100, 100, DefaultPixelType)] [WithTestPatternImages(nameof(DetectEdgesFilters), 100, 100, DefaultPixelType)]
[WithFileCollection(nameof(CommonTestImages), nameof(DetectEdgesFilters), DefaultPixelType)] [WithFileCollection(nameof(CommonTestImages), nameof(DetectEdgesFilters), DefaultPixelType)]
public void DetectEdges<TPixel>(TestImageProvider<TPixel> provider, EdgeDetection detector) public void DetectEdges_WorksWithAllFilters<TPixel>(TestImageProvider<TPixel> provider, EdgeDetection detector)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using (Image<TPixel> image = provider.GetImage())
@ -44,9 +45,33 @@ namespace ImageSharp.Tests.Processing.Processors.Convolution
} }
} }
[Theory]
[WithFileCollection(nameof(CommonTestImages), CommonNonDefaultPixelTypes)]
public void DetectEdges_IsNotBoundToSinglePixelType<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
image.Mutate(x => x.DetectEdges());
image.DebugSave(provider, grayscale: true);
}
}
[Theory]
[WithFile(TestImages.Gif.Giphy, DefaultPixelType)]
public void DetectEdges_IsAppliedToAllFrames<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
image.Mutate(x => x.DetectEdges());
image.DebugSave(provider, extension: "gif");
}
}
[Theory] [Theory]
[WithFileCollection(nameof(GrayscaleTestImages), DefaultPixelType)] [WithFileCollection(nameof(GrayscaleTestImages), DefaultPixelType)]
public void DetectEdgesInBox<TPixel>(TestImageProvider<TPixel> provider) public void DetectEdges_InBox<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> source = provider.GetImage()) using (Image<TPixel> source = provider.GetImage())

41
tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeTests.cs

@ -3,6 +3,7 @@
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
// </copyright> // </copyright>
// ReSharper disable InconsistentNaming
namespace ImageSharp.Tests.Processing.Processors.Transforms namespace ImageSharp.Tests.Processing.Processors.Transforms
{ {
using ImageSharp.PixelFormats; using ImageSharp.PixelFormats;
@ -34,23 +35,11 @@ namespace ImageSharp.Tests.Processing.Processors.Transforms
{ "Welch", new WelchResampler() } { "Welch", new WelchResampler() }
}; };
[Theory]
[WithFile(TestImages.Gif.Giphy, DefaultPixelType)]
public void ResizeShouldApplyToAllFrames<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
image.Mutate(x => x.Resize(image.Width / 2, image.Height / 2, true));
image.DebugSave(provider, extension: Extensions.Gif);
}
}
[Theory] [Theory]
[WithTestPatternImages(nameof(AllReSamplers), 100, 100, DefaultPixelType, 0.5f)] [WithTestPatternImages(nameof(AllReSamplers), 100, 100, DefaultPixelType, 0.5f)]
[WithFileCollection(nameof(CommonTestImages), nameof(AllReSamplers), DefaultPixelType, 0.5f)] [WithFileCollection(nameof(CommonTestImages), nameof(AllReSamplers), DefaultPixelType, 0.5f)]
[WithFileCollection(nameof(CommonTestImages), nameof(AllReSamplers), DefaultPixelType, 0.3f)] [WithFileCollection(nameof(CommonTestImages), nameof(AllReSamplers), DefaultPixelType, 0.3f)]
public void ResizeFullImage<TPixel>(TestImageProvider<TPixel> provider, string name, IResampler sampler, float ratio) public void Resize_WorksWithAllResamplers<TPixel>(TestImageProvider<TPixel> provider, string name, IResampler sampler, float ratio)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (Image<TPixel> image = provider.GetImage()) using (Image<TPixel> image = provider.GetImage())
@ -64,7 +53,7 @@ namespace ImageSharp.Tests.Processing.Processors.Transforms
[Theory] [Theory]
[WithTestPatternImages(100, 100, DefaultPixelType)] [WithTestPatternImages(100, 100, DefaultPixelType)]
public void ResizeFullImage_Compand<TPixel>(TestImageProvider<TPixel> provider) public void Resize_Compand<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())
@ -74,6 +63,30 @@ namespace ImageSharp.Tests.Processing.Processors.Transforms
} }
} }
[Theory]
[WithTestPatternImages(50, 50, CommonNonDefaultPixelTypes)]
public void Resize_IsNotBoundToSinglePixelType<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
image.Mutate(x => x.Resize(image.Width / 2, image.Height / 2, true));
image.DebugSave(provider);
}
}
[Theory]
[WithFile(TestImages.Gif.Giphy, DefaultPixelType)]
public void Resize_IsAppliedToAllFrames<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
image.Mutate(x => x.Resize(image.Width / 2, image.Height / 2, true));
image.DebugSave(provider, extension: Extensions.Gif);
}
}
[Theory] [Theory]
[WithFileCollection(nameof(GrayscaleTestImages), DefaultPixelType)] [WithFileCollection(nameof(GrayscaleTestImages), DefaultPixelType)]
public void ResizeFromSourceRectangle<TPixel>(TestImageProvider<TPixel> provider) public void ResizeFromSourceRectangle<TPixel>(TestImageProvider<TPixel> provider)

0
tests/ImageSharp.Tests/TestUtilities/ImageComparison/ImageDimensionsMismatchException.cs → tests/ImageSharp.Tests/TestUtilities/ImageComparison/Exceptions/ImageDimensionsMismatchException.cs

0
tests/ImageSharp.Tests/TestUtilities/ImageComparison/ImagePixelsAreDifferentException.cs → tests/ImageSharp.Tests/TestUtilities/ImageComparison/Exceptions/ImagePixelsAreDifferentException.cs

0
tests/ImageSharp.Tests/TestUtilities/ImageComparison/ImagesSimilarityException.cs → tests/ImageSharp.Tests/TestUtilities/ImageComparison/Exceptions/ImagesSimilarityException.cs

8
tests/ImageSharp.Tests/TestUtilities/PixelTypes.cs

@ -12,7 +12,7 @@ namespace ImageSharp.Tests
/// They trigger the desired parametrization for <see cref="TestImageProvider{TPixel}"/>. /// They trigger the desired parametrization for <see cref="TestImageProvider{TPixel}"/>.
/// </summary> /// </summary>
[Flags] [Flags]
public enum PixelTypes : uint public enum PixelTypes
{ {
Undefined = 0, Undefined = 0,
@ -52,11 +52,11 @@ namespace ImageSharp.Tests
Short4 = 1 << 17, Short4 = 1 << 17,
Rgb24 = 18, Rgb24 = 1 << 18,
Bgr24 = 19, Bgr24 = 1 << 19,
Bgra32 = 20, Bgra32 = 1 << 20,
// TODO: Add multi-flag entries by rules defined in PackedPixelConverterHelper // TODO: Add multi-flag entries by rules defined in PackedPixelConverterHelper

19
tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensions.cs

@ -103,7 +103,7 @@ namespace ImageSharp.Tests
return string.Join(separator, items.Select(o => string.Format(CultureInfo.InvariantCulture, "{0}", o))); return string.Join(separator, items.Select(o => string.Format(CultureInfo.InvariantCulture, "{0}", o)));
} }
public static Type ToType(this PixelTypes pixelType) => PixelTypes2ClrTypes[pixelType]; public static Type GetClrType(this PixelTypes pixelType) => PixelTypes2ClrTypes[pixelType];
/// <summary> /// <summary>
/// Returns the <see cref="PixelTypes"/> enumerations for the given type. /// Returns the <see cref="PixelTypes"/> enumerations for the given type.
@ -112,6 +112,8 @@ namespace ImageSharp.Tests
/// <returns></returns> /// <returns></returns>
public static PixelTypes GetPixelType(this Type colorStructClrType) => ClrTypes2PixelTypes[colorStructClrType]; public static PixelTypes GetPixelType(this Type colorStructClrType) => ClrTypes2PixelTypes[colorStructClrType];
public static IEnumerable<KeyValuePair<PixelTypes, Type>> ExpandAllTypes(this PixelTypes pixelTypes) public static IEnumerable<KeyValuePair<PixelTypes, Type>> ExpandAllTypes(this PixelTypes pixelTypes)
{ {
if (pixelTypes == PixelTypes.Undefined) if (pixelTypes == PixelTypes.Undefined)
@ -124,11 +126,20 @@ namespace ImageSharp.Tests
return PixelTypes2ClrTypes; return PixelTypes2ClrTypes;
} }
return AllConcretePixelTypes var result = new Dictionary<PixelTypes, Type>();
.Where(pt => pixelTypes.HasFlag(pt)) foreach (PixelTypes pt in AllConcretePixelTypes)
.Select(pt => new KeyValuePair<PixelTypes, Type>(pt, pt.ToType())); {
if (pixelTypes.HasAll(pt))
{
result[pt] = pt.GetClrType();
}
}
return result;
} }
internal static bool HasAll(this PixelTypes pixelTypes, PixelTypes flagsToCheck) =>
(pixelTypes & flagsToCheck) == flagsToCheck;
/// <summary> /// <summary>
/// Enumerate all available <see cref="PixelTypes"/>-s /// Enumerate all available <see cref="PixelTypes"/>-s
/// </summary> /// </summary>

27
tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs

@ -85,7 +85,7 @@ namespace ImageSharp.Tests
[InlineData(PixelTypes.Rgba32, typeof(Rgba32))] [InlineData(PixelTypes.Rgba32, typeof(Rgba32))]
public void ToType(PixelTypes pt, Type expectedType) public void ToType(PixelTypes pt, Type expectedType)
{ {
Assert.Equal(pt.ToType(), expectedType); Assert.Equal(pt.GetClrType(), expectedType);
} }
[Theory] [Theory]
@ -105,7 +105,7 @@ namespace ImageSharp.Tests
} }
[Fact] [Fact]
public void ToTypes() public void ExpandAllTypes_1()
{ {
PixelTypes pixelTypes = PixelTypes.Alpha8 | PixelTypes.Bgr565 | PixelTypes.HalfVector2 | PixelTypes.Rgba32; PixelTypes pixelTypes = PixelTypes.Alpha8 | PixelTypes.Bgr565 | PixelTypes.HalfVector2 | PixelTypes.Rgba32;
@ -119,6 +119,29 @@ namespace ImageSharp.Tests
AssertContainsPixelType<Rgba32>(PixelTypes.Rgba32, expanded); AssertContainsPixelType<Rgba32>(PixelTypes.Rgba32, expanded);
} }
[Fact]
public void ExpandAllTypes_2()
{
PixelTypes pixelTypes = PixelTypes.Rgba32 | PixelTypes.Bgra32 | PixelTypes.RgbaVector;
IEnumerable<KeyValuePair<PixelTypes, Type>> expanded = pixelTypes.ExpandAllTypes();
Assert.Equal(3, expanded.Count());
AssertContainsPixelType<Rgba32>(PixelTypes.Rgba32, expanded);
AssertContainsPixelType<Bgra32>(PixelTypes.Bgra32, expanded);
AssertContainsPixelType<RgbaVector>(PixelTypes.RgbaVector, expanded);
}
[Fact]
public void Anyad()
{
PixelTypes pixelTypes = PixelTypes.Rgba32 | PixelTypes.Bgra32 | PixelTypes.RgbaVector;
PixelTypes anyad = pixelTypes & PixelTypes.Bgr565;
this.Output.WriteLine(anyad.ToString());
}
[Fact] [Fact]
public void ToTypes_All() public void ToTypes_All()
{ {

Loading…
Cancel
Save