Skip to content

Commit e76add2

Browse files
authored
Merge pull request #902 from SixLabors/af/extend-processor-coverage
Validating tests for Image Processors
2 parents b0f6b73 + 4f41288 commit e76add2

File tree

21 files changed

+422
-503
lines changed

21 files changed

+422
-503
lines changed

src/ImageSharp/Common/Helpers/Vector4Utils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public static void Transform(ref Vector4 vector, ref ColorMatrix matrix)
9292
}
9393

9494
/// <summary>
95-
/// Bulk variant of <see cref="Transform(ref Vector4, ref ColorMatrix)"/>
95+
/// Bulk variant of <see cref="Transform(ref Vector4, ref ColorMatrix)"/>.
9696
/// </summary>
9797
/// <param name="vectors">The span of vectors</param>
9898
/// <param name="matrix">The transformation matrix.</param>

src/ImageSharp/Processing/Processors/Convolution/EdgeDetector2DProcessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ protected EdgeDetector2DProcessor(in DenseMatrix<float> kernelX, in DenseMatrix<
4040
public DenseMatrix<float> KernelY { get; }
4141

4242
/// <inheritdoc/>
43-
public bool Grayscale { get; set; }
43+
public bool Grayscale { get; }
4444

4545
/// <inheritdoc />
4646
protected override void OnFrameApply(ImageFrame<TPixel> source, Rectangle sourceRectangle, Configuration configuration)

tests/ImageSharp.Tests/FileTestBase.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
// Copyright (c) Six Labors and contributors.
22
// Licensed under the Apache License, Version 2.0.
33

4+
using System;
45
using System.Collections.Generic;
56

67
namespace SixLabors.ImageSharp.Tests
78
{
89
/// <summary>
910
/// The test base class for reading and writing to files.
1011
/// </summary>
12+
[Obsolete("See: https://github.com/SixLabors/ImageSharp/issues/868")]
1113
public abstract class FileTestBase
1214
{
1315
/// <summary>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// // Copyright (c) Six Labors and contributors.
2+
// // Licensed under the Apache License, Version 2.0.
3+
4+
using SixLabors.ImageSharp.PixelFormats;
5+
using SixLabors.ImageSharp.Processing;
6+
using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison;
7+
using SixLabors.Primitives;
8+
9+
using Xunit;
10+
11+
namespace SixLabors.ImageSharp.Tests.Processing.Processors.Convolution
12+
{
13+
[GroupOutput("Convolution")]
14+
public abstract class Basic1ParameterConvolutionTests
15+
{
16+
private static readonly ImageComparer ValidatorComparer = ImageComparer.TolerantPercentage(0.05F);
17+
18+
public static readonly TheoryData<int> Values = new TheoryData<int> { 3, 5 };
19+
20+
public static readonly string[] InputImages =
21+
{
22+
TestImages.Bmp.Car,
23+
TestImages.Png.CalliphoraPartial
24+
};
25+
26+
[Theory]
27+
[WithFileCollection(nameof(InputImages), nameof(Values), PixelTypes.Rgba32)]
28+
public void OnFullImage<TPixel>(TestImageProvider<TPixel> provider, int value)
29+
where TPixel : struct, IPixel<TPixel>
30+
{
31+
provider.Utility.TestGroupName = this.GetType().Name;
32+
provider.RunValidatingProcessorTest(
33+
x => this.Apply(x, value),
34+
value,
35+
ValidatorComparer);
36+
}
37+
38+
[Theory]
39+
[WithFileCollection(nameof(InputImages), nameof(Values), PixelTypes.Rgba32)]
40+
public void InBox<TPixel>(TestImageProvider<TPixel> provider, int value)
41+
where TPixel : struct, IPixel<TPixel>
42+
{
43+
provider.Utility.TestGroupName = this.GetType().Name;
44+
provider.RunRectangleConstrainedValidatingProcessorTest(
45+
(x, rect) => this.Apply(x, value, rect),
46+
value,
47+
ValidatorComparer);
48+
}
49+
50+
protected abstract void Apply<TPixel>(IImageProcessingContext<TPixel> ctx, int value)
51+
where TPixel : struct, IPixel<TPixel>;
52+
53+
protected abstract void Apply<TPixel>(IImageProcessingContext<TPixel> ctx, int value, Rectangle bounds)
54+
where TPixel : struct, IPixel<TPixel>;
55+
}
56+
}
Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,17 @@
11
// Copyright (c) Six Labors and contributors.
22
// Licensed under the Apache License, Version 2.0.
33

4-
using SixLabors.ImageSharp.PixelFormats;
54
using SixLabors.ImageSharp.Processing;
6-
using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison;
7-
85
using SixLabors.Primitives;
9-
using Xunit;
106

117
namespace SixLabors.ImageSharp.Tests.Processing.Processors.Convolution
128
{
13-
public class BoxBlurTest : FileTestBase
9+
[GroupOutput("Convolution")]
10+
public class BoxBlurTest : Basic1ParameterConvolutionTests
1411
{
15-
public static readonly TheoryData<int> BoxBlurValues
16-
= new TheoryData<int>
17-
{
18-
3,
19-
5
20-
};
21-
22-
[Theory]
23-
[WithFileCollection(nameof(DefaultFiles), nameof(BoxBlurValues), DefaultPixelType)]
24-
public void ImageShouldApplyBoxBlurFilter<TPixel>(TestImageProvider<TPixel> provider, int value)
25-
where TPixel : struct, IPixel<TPixel>
26-
{
27-
using (Image<TPixel> image = provider.GetImage())
28-
{
29-
image.Mutate(x => x.BoxBlur(value));
30-
image.DebugSave(provider, value);
31-
}
32-
}
33-
34-
[Theory]
35-
[WithFileCollection(nameof(DefaultFiles), nameof(BoxBlurValues), DefaultPixelType)]
36-
public void ImageShouldApplyBoxBlurFilterInBox<TPixel>(TestImageProvider<TPixel> provider, int value)
37-
where TPixel : struct, IPixel<TPixel>
38-
{
39-
using (Image<TPixel> source = provider.GetImage())
40-
using (Image<TPixel> image = source.Clone())
41-
{
42-
var bounds = new Rectangle(10, 10, image.Width / 2, image.Height / 2);
43-
44-
image.Mutate(x => x.BoxBlur(value, bounds));
45-
image.DebugSave(provider, value);
12+
protected override void Apply<TPixel>(IImageProcessingContext<TPixel> ctx, int value) => ctx.BoxBlur(value);
4613

47-
ImageComparer.Tolerant().VerifySimilarityIgnoreRegion(source, image, bounds);
48-
}
49-
}
14+
protected override void Apply<TPixel>(IImageProcessingContext<TPixel> ctx, int value, Rectangle bounds) =>
15+
ctx.BoxBlur(value, bounds);
5016
}
5117
}

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@
1010

1111
namespace SixLabors.ImageSharp.Tests.Processing.Processors.Convolution
1212
{
13-
public class DetectEdgesTest : FileTestBase
13+
[GroupOutput("Convolution")]
14+
public class DetectEdgesTest
1415
{
1516
// I think our comparison is not accurate enough (nor can be) for RgbaVector.
1617
// The image pixels are identical according to BeyondCompare.
1718
private static readonly ImageComparer ValidatorComparer = ImageComparer.TolerantPercentage(0.0456F);
1819

19-
public static readonly string[] CommonTestImages = { TestImages.Png.Bike };
20+
public static readonly string[] TestImages = { Tests.TestImages.Png.Bike };
21+
22+
public const PixelTypes CommonNonDefaultPixelTypes = PixelTypes.Rgba32 | PixelTypes.Bgra32 | PixelTypes.RgbaVector;
2023

2124
public static readonly TheoryData<EdgeDetectionOperators> DetectEdgesFilters = new TheoryData<EdgeDetectionOperators>
2225
{
@@ -33,7 +36,7 @@ public class DetectEdgesTest : FileTestBase
3336
};
3437

3538
[Theory]
36-
[WithFileCollection(nameof(CommonTestImages), DefaultPixelType)]
39+
[WithFileCollection(nameof(TestImages), PixelTypes.Rgba32)]
3740
public void DetectEdges_WorksOnWrappedMemoryImage<TPixel>(TestImageProvider<TPixel> provider)
3841
where TPixel : struct, IPixel<TPixel>
3942
{
@@ -49,8 +52,8 @@ public void DetectEdges_WorksOnWrappedMemoryImage<TPixel>(TestImageProvider<TPix
4952
}
5053

5154
[Theory]
52-
[WithTestPatternImages(nameof(DetectEdgesFilters), 100, 100, DefaultPixelType)]
53-
[WithFileCollection(nameof(CommonTestImages), nameof(DetectEdgesFilters), DefaultPixelType)]
55+
[WithTestPatternImages(nameof(DetectEdgesFilters), 100, 100, PixelTypes.Rgba32)]
56+
[WithFileCollection(nameof(TestImages), nameof(DetectEdgesFilters), PixelTypes.Rgba32)]
5457
public void DetectEdges_WorksWithAllFilters<TPixel>(TestImageProvider<TPixel> provider, EdgeDetectionOperators detector)
5558
where TPixel : struct, IPixel<TPixel>
5659
{
@@ -63,7 +66,7 @@ public void DetectEdges_WorksWithAllFilters<TPixel>(TestImageProvider<TPixel> pr
6366
}
6467

6568
[Theory]
66-
[WithFileCollection(nameof(CommonTestImages), CommonNonDefaultPixelTypes)]
69+
[WithFileCollection(nameof(TestImages), CommonNonDefaultPixelTypes)]
6770
public void DetectEdges_IsNotBoundToSinglePixelType<TPixel>(TestImageProvider<TPixel> provider)
6871
where TPixel : struct, IPixel<TPixel>
6972
{
@@ -76,7 +79,7 @@ public void DetectEdges_IsNotBoundToSinglePixelType<TPixel>(TestImageProvider<TP
7679
}
7780

7881
[Theory]
79-
[WithFile(TestImages.Gif.Giphy, DefaultPixelType)]
82+
[WithFile(Tests.TestImages.Gif.Giphy, PixelTypes.Rgba32)]
8083
public void DetectEdges_IsAppliedToAllFrames<TPixel>(TestImageProvider<TPixel> provider)
8184
where TPixel : struct, IPixel<TPixel>
8285
{
@@ -88,7 +91,7 @@ public void DetectEdges_IsAppliedToAllFrames<TPixel>(TestImageProvider<TPixel> p
8891
}
8992

9093
[Theory]
91-
[WithFileCollection(nameof(CommonTestImages), DefaultPixelType)]
94+
[WithFileCollection(nameof(TestImages), PixelTypes.Rgba32)]
9295
public void DetectEdges_InBox<TPixel>(TestImageProvider<TPixel> provider)
9396
where TPixel : struct, IPixel<TPixel>
9497
{

tests/ImageSharp.Tests/Processing/Processors/Convolution/GaussianBlurTest.cs

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,37 +10,12 @@
1010

1111
namespace SixLabors.ImageSharp.Tests.Processing.Processors.Convolution
1212
{
13-
public class GaussianBlurTest : FileTestBase
13+
[GroupOutput("Convolution")]
14+
public class GaussianBlurTest : Basic1ParameterConvolutionTests
1415
{
15-
public static readonly TheoryData<int> GaussianBlurValues = new TheoryData<int> { 3, 5 };
16+
protected override void Apply<TPixel>(IImageProcessingContext<TPixel> ctx, int value) => ctx.GaussianBlur(value);
1617

17-
[Theory]
18-
[WithFileCollection(nameof(DefaultFiles), nameof(GaussianBlurValues), DefaultPixelType)]
19-
public void ImageShouldApplyGaussianBlurFilter<TPixel>(TestImageProvider<TPixel> provider, int value)
20-
where TPixel : struct, IPixel<TPixel>
21-
{
22-
using (Image<TPixel> image = provider.GetImage())
23-
{
24-
image.Mutate(x => x.GaussianBlur(value));
25-
image.DebugSave(provider, value);
26-
}
27-
}
28-
29-
[Theory]
30-
[WithFileCollection(nameof(DefaultFiles), nameof(GaussianBlurValues), DefaultPixelType)]
31-
public void ImageShouldApplyGaussianBlurFilterInBox<TPixel>(TestImageProvider<TPixel> provider, int value)
32-
where TPixel : struct, IPixel<TPixel>
33-
{
34-
using (Image<TPixel> source = provider.GetImage())
35-
using (Image<TPixel> image = source.Clone())
36-
{
37-
var bounds = new Rectangle(10, 10, image.Width / 2, image.Height / 2);
38-
39-
image.Mutate(x => x.GaussianBlur(value, bounds));
40-
image.DebugSave(provider, value);
41-
42-
ImageComparer.Tolerant().VerifySimilarityIgnoreRegion(source, image, bounds);
43-
}
44-
}
18+
protected override void Apply<TPixel>(IImageProcessingContext<TPixel> ctx, int value, Rectangle bounds) =>
19+
ctx.GaussianBlur(value, bounds);
4520
}
4621
}

tests/ImageSharp.Tests/Processing/Processors/Convolution/GaussianSharpenTest.cs

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,42 +9,12 @@
99

1010
namespace SixLabors.ImageSharp.Tests.Processing.Processors.Convolution
1111
{
12-
public class GaussianSharpenTest : FileTestBase
12+
[GroupOutput("Convolution")]
13+
public class GaussianSharpenTest : Basic1ParameterConvolutionTests
1314
{
14-
public static readonly TheoryData<int> GaussianSharpenValues
15-
= new TheoryData<int>
16-
{
17-
3,
18-
5
19-
};
15+
protected override void Apply<TPixel>(IImageProcessingContext<TPixel> ctx, int value) => ctx.GaussianSharpen(value);
2016

21-
[Theory]
22-
[WithFileCollection(nameof(DefaultFiles), nameof(GaussianSharpenValues), DefaultPixelType)]
23-
public void ImageShouldApplyGaussianSharpenFilter<TPixel>(TestImageProvider<TPixel> provider, int value)
24-
where TPixel : struct, IPixel<TPixel>
25-
{
26-
using (Image<TPixel> image = provider.GetImage())
27-
{
28-
image.Mutate(x => x.GaussianSharpen(value));
29-
image.DebugSave(provider, value);
30-
}
31-
}
32-
33-
[Theory]
34-
[WithFileCollection(nameof(DefaultFiles), nameof(GaussianSharpenValues), DefaultPixelType)]
35-
public void ImageShouldApplyGaussianSharpenFilterInBox<TPixel>(TestImageProvider<TPixel> provider, int value)
36-
where TPixel : struct, IPixel<TPixel>
37-
{
38-
using (Image<TPixel> source = provider.GetImage())
39-
using (var image = source.Clone())
40-
{
41-
var bounds = new Rectangle(10, 10, image.Width / 2, image.Height / 2);
42-
43-
image.Mutate(x => x.GaussianSharpen(value, bounds));
44-
image.DebugSave(provider, value);
45-
46-
ImageComparer.Tolerant().VerifySimilarityIgnoreRegion(source, image, bounds);
47-
}
48-
}
17+
protected override void Apply<TPixel>(IImageProcessingContext<TPixel> ctx, int value, Rectangle bounds) =>
18+
ctx.GaussianSharpen(value, bounds);
4919
}
5020
}

0 commit comments

Comments
 (0)