|
|
|
@ -10,24 +10,36 @@ namespace SixLabors.ImageSharp.Tests.Quantization; |
|
|
|
|
|
|
|
public class PixelSamplingStrategyTests |
|
|
|
{ |
|
|
|
public static readonly TheoryData<int, int, int, int> DefaultPixelSamplingStrategy_Data = new TheoryData<int, int, int, int>() |
|
|
|
{ |
|
|
|
{ 100, 100, 1, 10000 }, |
|
|
|
{ 100, 100, 1, 5000 }, |
|
|
|
{ 100, 100, 10, 50000 }, |
|
|
|
{ 99, 100, 11, 30000 }, |
|
|
|
{ 97, 99, 11, 80000 }, |
|
|
|
{ 99, 100, 11, 20000 }, |
|
|
|
{ 99, 501, 20, 100000 }, |
|
|
|
{ 97, 500, 20, 10000 }, |
|
|
|
{ 103, 501, 20, 1000 }, |
|
|
|
}; |
|
|
|
public static readonly TheoryData<int, int, int, int> DefaultPixelSamplingStrategy_MultiFrame_Data = new() |
|
|
|
{ |
|
|
|
{ 100, 100, 1, 10000 }, |
|
|
|
{ 100, 100, 1, 5000 }, |
|
|
|
{ 100, 100, 10, 50000 }, |
|
|
|
{ 99, 100, 11, 30000 }, |
|
|
|
{ 97, 99, 11, 80000 }, |
|
|
|
{ 99, 100, 11, 20000 }, |
|
|
|
{ 99, 501, 20, 100000 }, |
|
|
|
{ 97, 500, 20, 10000 }, |
|
|
|
{ 103, 501, 20, 1000 }, |
|
|
|
}; |
|
|
|
|
|
|
|
public static readonly TheoryData<int, int, int> DefaultPixelSamplingStrategy_Data = new() |
|
|
|
{ |
|
|
|
{ 100, 100, 9900 }, |
|
|
|
{ 100, 100, 5000 }, |
|
|
|
{ 99, 100, 30000 }, |
|
|
|
{ 97, 99, 80000 }, |
|
|
|
{ 99, 100, 20000 }, |
|
|
|
{ 99, 501, 100000 }, |
|
|
|
{ 97, 500, 10000 }, |
|
|
|
{ 103, 501, 1000 }, |
|
|
|
}; |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void ExtensivePixelSamplingStrategy_EnumeratesAll() |
|
|
|
{ |
|
|
|
using Image<L8> image = CreateTestImage(100, 100, 100); |
|
|
|
var strategy = new ExtensivePixelSamplingStrategy(); |
|
|
|
ExtensivePixelSamplingStrategy strategy = new(); |
|
|
|
|
|
|
|
foreach (Buffer2DRegion<L8> region in strategy.EnumeratePixelRegions(image)) |
|
|
|
{ |
|
|
|
@ -40,12 +52,12 @@ public class PixelSamplingStrategyTests |
|
|
|
} |
|
|
|
|
|
|
|
[Theory] |
|
|
|
[WithBlankImages(nameof(DefaultPixelSamplingStrategy_Data), 1, 1, PixelTypes.L8)] |
|
|
|
public void DefaultPixelSamplingStrategy_IsFair(TestImageProvider<L8> dummyProvider, int width, int height, int noOfFrames, int maximumNumberOfPixels) |
|
|
|
[WithBlankImages(nameof(DefaultPixelSamplingStrategy_MultiFrame_Data), 1, 1, PixelTypes.L8)] |
|
|
|
public void DefaultPixelSamplingStrategy_IsFair_MultiFrame(TestImageProvider<L8> dummyProvider, int width, int height, int noOfFrames, int maximumNumberOfPixels) |
|
|
|
{ |
|
|
|
using Image<L8> image = CreateTestImage(width, height, noOfFrames); |
|
|
|
|
|
|
|
var strategy = new DefaultPixelSamplingStrategy(maximumNumberOfPixels, 0.1); |
|
|
|
DefaultPixelSamplingStrategy strategy = new(maximumNumberOfPixels, 0.1); |
|
|
|
|
|
|
|
long visitedPixels = 0; |
|
|
|
foreach (Buffer2DRegion<L8> region in strategy.EnumeratePixelRegions(image)) |
|
|
|
@ -67,9 +79,37 @@ public class PixelSamplingStrategyTests |
|
|
|
Assert.True(visitRatio <= 1.1, $"{visitedPixels}>{maximumPixels}"); |
|
|
|
} |
|
|
|
|
|
|
|
[Theory] |
|
|
|
[WithBlankImages(nameof(DefaultPixelSamplingStrategy_Data), 1, 1, PixelTypes.L8)] |
|
|
|
public void DefaultPixelSamplingStrategy_IsFair(TestImageProvider<L8> dummyProvider, int width, int height, int maximumNumberOfPixels) |
|
|
|
{ |
|
|
|
using Image<L8> image = CreateTestImage(width, height, 1); |
|
|
|
|
|
|
|
DefaultPixelSamplingStrategy strategy = new(maximumNumberOfPixels, 0.1); |
|
|
|
|
|
|
|
long visitedPixels = 0; |
|
|
|
foreach (Buffer2DRegion<L8> region in strategy.EnumeratePixelRegions(image)) |
|
|
|
{ |
|
|
|
PaintWhite(region); |
|
|
|
visitedPixels += region.Width * region.Height; |
|
|
|
} |
|
|
|
|
|
|
|
image.DebugSave( |
|
|
|
dummyProvider, |
|
|
|
$"W{width}_H{height}_maximumNumberOfPixels_{maximumNumberOfPixels}", |
|
|
|
appendPixelTypeToFileName: false); |
|
|
|
|
|
|
|
int maximumPixels = image.Width * image.Height * image.Frames.Count / 10; |
|
|
|
maximumPixels = Math.Max(maximumPixels, (int)strategy.MaximumPixels); |
|
|
|
|
|
|
|
// allow some inaccuracy:
|
|
|
|
double visitRatio = visitedPixels / (double)maximumPixels; |
|
|
|
Assert.True(visitRatio <= 1.1, $"{visitedPixels}>{maximumPixels}"); |
|
|
|
} |
|
|
|
|
|
|
|
private static void PaintWhite(Buffer2DRegion<L8> region) |
|
|
|
{ |
|
|
|
var white = new L8(255); |
|
|
|
L8 white = new(255); |
|
|
|
for (int y = 0; y < region.Height; y++) |
|
|
|
{ |
|
|
|
region.DangerousGetRowSpan(y).Fill(white); |
|
|
|
@ -79,7 +119,7 @@ public class PixelSamplingStrategyTests |
|
|
|
private static Image<L8> CreateTestImage(int width, int height, int noOfFrames, bool paintWhite = false) |
|
|
|
{ |
|
|
|
L8 bg = paintWhite ? new L8(255) : default; |
|
|
|
var image = new Image<L8>(width, height, bg); |
|
|
|
Image<L8> image = new(width, height, bg); |
|
|
|
|
|
|
|
for (int i = 1; i < noOfFrames; i++) |
|
|
|
{ |
|
|
|
|