📷 A modern, cross-platform, 2D Graphics library for .NET
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

127 lines
5.4 KiB

// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison;
using Xunit;
// ReSharper disable InconsistentNaming
namespace SixLabors.ImageSharp.Tests.Processing.Processors.Convolution
{
[GroupOutput("Convolution")]
public class DetectEdgesTest
{
private static readonly ImageComparer OpaqueComparer = ImageComparer.TolerantPercentage(0.01F);
private static readonly ImageComparer TransparentComparer = ImageComparer.TolerantPercentage(0.5F);
public static readonly string[] TestImages = { Tests.TestImages.Png.Bike };
public const PixelTypes CommonNonDefaultPixelTypes = PixelTypes.Rgba32 | PixelTypes.Bgra32 | PixelTypes.RgbaVector;
public static readonly TheoryData<KnownEdgeDetectionOperators> DetectEdgesFilters = new TheoryData<KnownEdgeDetectionOperators>
{
KnownEdgeDetectionOperators.Kayyali,
KnownEdgeDetectionOperators.Kirsch,
KnownEdgeDetectionOperators.Laplacian3x3,
KnownEdgeDetectionOperators.Laplacian5x5,
KnownEdgeDetectionOperators.LaplacianOfGaussian,
KnownEdgeDetectionOperators.Prewitt,
KnownEdgeDetectionOperators.RobertsCross,
KnownEdgeDetectionOperators.Robinson,
KnownEdgeDetectionOperators.Scharr,
KnownEdgeDetectionOperators.Sobel
};
[Theory]
[WithFileCollection(nameof(TestImages), PixelTypes.Rgba32)]
public void DetectEdges_WorksOnWrappedMemoryImage<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
provider.RunValidatingProcessorTestOnWrappedMemoryImage(
ctx =>
{
Size size = ctx.GetCurrentSize();
var bounds = new Rectangle(10, 10, size.Width / 2, size.Height / 2);
ctx.DetectEdges(bounds);
},
comparer: OpaqueComparer,
useReferenceOutputFrom: nameof(this.DetectEdges_InBox));
}
[Theory]
[WithTestPatternImages(nameof(DetectEdgesFilters), 100, 100, PixelTypes.Rgba32)]
[WithFileCollection(nameof(TestImages), nameof(DetectEdgesFilters), PixelTypes.Rgba32)]
public void DetectEdges_WorksWithAllFilters<TPixel>(TestImageProvider<TPixel> provider, KnownEdgeDetectionOperators detector)
where TPixel : unmanaged, IPixel<TPixel>
{
bool hasAlpha = provider.SourceFileOrDescription.Contains("TestPattern");
ImageComparer comparer = hasAlpha ? TransparentComparer : OpaqueComparer;
using (Image<TPixel> image = provider.GetImage())
{
image.Mutate(x => x.DetectEdges(detector));
image.DebugSave(provider, detector.ToString());
image.CompareToReferenceOutput(comparer, provider, detector.ToString());
}
}
[Theory]
[WithFileCollection(nameof(TestImages), CommonNonDefaultPixelTypes)]
public void DetectEdges_IsNotBoundToSinglePixelType<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
// James:
// I think our comparison is not accurate enough (nor can be) for RgbaVector.
// The image pixels are identical according to BeyondCompare.
ImageComparer comparer = typeof(TPixel) == typeof(RgbaVector) ?
ImageComparer.TolerantPercentage(1f) :
OpaqueComparer;
using (Image<TPixel> image = provider.GetImage())
{
image.Mutate(x => x.DetectEdges());
image.DebugSave(provider);
image.CompareToReferenceOutput(comparer, provider);
}
}
[Theory]
[WithFile(Tests.TestImages.Gif.Giphy, PixelTypes.Rgba32)]
public void DetectEdges_IsAppliedToAllFrames<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
image.Mutate(x => x.DetectEdges());
image.DebugSave(provider, extension: "gif");
}
}
[Theory]
[WithFileCollection(nameof(TestImages), PixelTypes.Rgba32)]
public void DetectEdges_InBox<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
var bounds = new Rectangle(10, 10, image.Width / 2, image.Height / 2);
image.Mutate(x => x.DetectEdges(bounds));
image.DebugSave(provider);
image.CompareToReferenceOutput(OpaqueComparer, provider);
}
}
[Theory]
[WithFile(Tests.TestImages.Png.Bike, nameof(DetectEdgesFilters), PixelTypes.Rgba32)]
public void WorksWithDiscoBuffers<TPixel>(TestImageProvider<TPixel> provider, KnownEdgeDetectionOperators detector)
where TPixel : unmanaged, IPixel<TPixel>
{
provider.RunBufferCapacityLimitProcessorTest(
41,
c => c.DetectEdges(detector),
detector);
}
}
}