Browse Source

PixelTypes bugfix + polishing ResizeTests

pull/298/head
Anton Firszov 9 years ago
parent
commit
105d445cd4
  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>
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 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.
// </copyright>
// ReSharper disable InconsistentNaming
namespace ImageSharp.Tests.Processing.Processors.Convolution
{
using ImageSharp.PixelFormats;
@ -34,7 +35,7 @@ namespace ImageSharp.Tests.Processing.Processors.Convolution
[Theory]
[WithTestPatternImages(nameof(DetectEdgesFilters), 100, 100, 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>
{
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]
[WithFileCollection(nameof(GrayscaleTestImages), DefaultPixelType)]
public void DetectEdgesInBox<TPixel>(TestImageProvider<TPixel> provider)
public void DetectEdges_InBox<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel>
{
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.
// </copyright>
// ReSharper disable InconsistentNaming
namespace ImageSharp.Tests.Processing.Processors.Transforms
{
using ImageSharp.PixelFormats;
@ -34,23 +35,11 @@ namespace ImageSharp.Tests.Processing.Processors.Transforms
{ "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]
[WithTestPatternImages(nameof(AllReSamplers), 100, 100, DefaultPixelType, 0.5f)]
[WithFileCollection(nameof(CommonTestImages), nameof(AllReSamplers), DefaultPixelType, 0.5f)]
[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>
{
using (Image<TPixel> image = provider.GetImage())
@ -64,7 +53,7 @@ namespace ImageSharp.Tests.Processing.Processors.Transforms
[Theory]
[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>
{
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]
[WithFileCollection(nameof(GrayscaleTestImages), DefaultPixelType)]
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}"/>.
/// </summary>
[Flags]
public enum PixelTypes : uint
public enum PixelTypes
{
Undefined = 0,
@ -52,11 +52,11 @@ namespace ImageSharp.Tests
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

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

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

@ -85,7 +85,7 @@ namespace ImageSharp.Tests
[InlineData(PixelTypes.Rgba32, typeof(Rgba32))]
public void ToType(PixelTypes pt, Type expectedType)
{
Assert.Equal(pt.ToType(), expectedType);
Assert.Equal(pt.GetClrType(), expectedType);
}
[Theory]
@ -105,7 +105,7 @@ namespace ImageSharp.Tests
}
[Fact]
public void ToTypes()
public void ExpandAllTypes_1()
{
PixelTypes pixelTypes = PixelTypes.Alpha8 | PixelTypes.Bgr565 | PixelTypes.HalfVector2 | PixelTypes.Rgba32;
@ -119,6 +119,29 @@ namespace ImageSharp.Tests
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]
public void ToTypes_All()
{

Loading…
Cancel
Save