forked from Nominom/BCnEncoder.NET
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMipMapperTests.cs
More file actions
79 lines (67 loc) · 2.45 KB
/
Copy pathMipMapperTests.cs
File metadata and controls
79 lines (67 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System;
using BCnEncoder.Shared;
using BCnEncTests.Support;
using CommunityToolkit.HighPerformance;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
using Xunit;
using Xunit.Abstractions;
namespace BCnEncTests
{
public class MipMapperTests
{
private readonly ITestOutputHelper output;
public MipMapperTests(ITestOutputHelper output) => this.output = output;
[Fact]
public void MipChainHasCorrectDimensions()
{
var image = ImageLoader.TestGradient1; // 512x416
var numMips = 0;
var chain = MipMapper.GenerateMipChain(image, ref numMips);
Assert.Equal(chain.Length, numMips);
Assert.True(numMips > 1);
for (var i = 0; i < numMips; i++)
{
Assert.Equal(Math.Max(1, image.Width >> i), chain[i].Width);
Assert.Equal(Math.Max(1, image.Height >> i), chain[i].Height);
}
// Last level must be 1x1
Assert.Equal(1, chain[numMips - 1].Width);
Assert.Equal(1, chain[numMips - 1].Height);
}
/// <summary>
/// Compares each mip level produced by MipMapper against the equivalent
/// ImageSharp Box-filter resize (Compand=false so both operate in sRGB
/// byte space without gamma correction). The PSNR should be very high
/// because both algorithms perform the same 2x2 box average.
/// </summary>
[Theory]
[InlineData(1)]
[InlineData(2)]
[InlineData(3)]
[InlineData(4)]
public void MipLevelMatchesImageSharpBoxFilter(int mipLevel)
{
var image = ImageLoader.TestGradient1;
var numMips = mipLevel + 1;
var chain = MipMapper.GenerateMipChain(image, ref numMips);
var mipW = chain[mipLevel].Width;
var mipH = chain[mipLevel].Height;
// ImageSharp Box sampler + Compand=false: no gamma correction,
// averages pixel values in sRGB space — matches MipMapper exactly.
using var imageSharp = ImageLoader.LoadTestImageSharp("../../../testImages/test_gradient_1_512.jpg");
using var reference = imageSharp.Clone(x => x.Resize(new ResizeOptions
{
Size = new Size(mipW, mipH),
Sampler = KnownResamplers.Box,
Compand = false
}));
var mipColors = TestHelper.GetSinglePixelArrayAsColors(chain[mipLevel]);
var refColors = TestHelper.GetSinglePixelArrayAsColors(reference);
var psnr = ImageQuality.PeakSignalToNoiseRatio(mipColors, refColors);
output.WriteLine($"Mip level {mipLevel} ({mipW}x{mipH}): PSNR vs ImageSharp Box = {psnr:F2} dB");
Assert.True(psnr > 40,
$"Mip level {mipLevel}: PSNR vs ImageSharp Box was {psnr:F2} dB (expected > 40)");
}
}
}