2023-01-21 19:36:12 +00:00
|
|
|
using System;
|
2022-12-28 16:21:58 +00:00
|
|
|
using System.IO;
|
2023-01-21 19:36:12 +00:00
|
|
|
using System.Runtime.InteropServices;
|
2022-12-28 16:21:58 +00:00
|
|
|
using System.Threading;
|
2021-09-27 12:42:46 +00:00
|
|
|
using System.Threading.Tasks;
|
2022-12-28 16:21:58 +00:00
|
|
|
using FluentAssertions;
|
2021-09-27 12:42:46 +00:00
|
|
|
using Shipwreck.Phash;
|
|
|
|
using Wabbajack.DTOs.Texture;
|
|
|
|
using Wabbajack.Paths;
|
|
|
|
using Wabbajack.Paths.IO;
|
|
|
|
using Xunit;
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
namespace Wabbajack.Hashing.PHash.Test;
|
|
|
|
|
2023-01-21 19:36:12 +00:00
|
|
|
public class FileLoadingTests : IAsyncDisposable
|
2021-09-27 12:42:46 +00:00
|
|
|
{
|
2023-01-21 19:36:12 +00:00
|
|
|
private readonly IImageLoader[] _imageLoaders;
|
|
|
|
private readonly TemporaryFileManager _tmp;
|
|
|
|
|
|
|
|
public FileLoadingTests()
|
|
|
|
{
|
|
|
|
_tmp = new TemporaryFileManager();
|
|
|
|
|
|
|
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
|
|
|
|
_imageLoaders = new IImageLoader[]
|
|
|
|
{
|
|
|
|
new CrossPlatformImageLoader(),
|
|
|
|
new TexConvImageLoader(_tmp)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_imageLoaders = new IImageLoader[]
|
|
|
|
{
|
|
|
|
new CrossPlatformImageLoader(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
[Theory]
|
|
|
|
[InlineData("test-dxt5.dds", 1.0f)]
|
|
|
|
[InlineData("test-dxt5-recompressed.dds", 1f)]
|
|
|
|
[InlineData("test-dxt5-small-bc7.dds", 0.983f)]
|
|
|
|
[InlineData("test-dxt5-small-bc7-vflip.dds", 0.189f)]
|
|
|
|
public async Task LoadAllFiles(string file, float difference)
|
2021-09-27 12:42:46 +00:00
|
|
|
{
|
2023-01-21 19:36:12 +00:00
|
|
|
foreach (var imageLoader in _imageLoaders)
|
|
|
|
{
|
|
|
|
var baseState =
|
|
|
|
await imageLoader.Load("TestData/test-dxt5.dds".ToRelativePath().RelativeTo(KnownFolders.EntryPoint));
|
|
|
|
var state = await imageLoader.Load("TestData".ToRelativePath().Combine(file)
|
|
|
|
.RelativeTo(KnownFolders.EntryPoint));
|
|
|
|
|
|
|
|
Assert.NotEqual(DXGI_FORMAT.UNKNOWN, baseState.Format);
|
|
|
|
|
|
|
|
Assert.Equal(difference,
|
|
|
|
ImagePhash.GetCrossCorrelation(
|
|
|
|
new Digest { Coefficients = baseState.PerceptualHash.Data },
|
|
|
|
new Digest { Coefficients = state.PerceptualHash.Data }),
|
|
|
|
1.0);
|
|
|
|
}
|
2021-09-27 12:42:46 +00:00
|
|
|
}
|
2022-12-28 16:21:58 +00:00
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public async Task CanConvertCubeMaps()
|
|
|
|
{
|
2023-01-21 19:36:12 +00:00
|
|
|
foreach (var imageLoader in _imageLoaders)
|
|
|
|
{
|
|
|
|
// File used here via re-upload permissions found on the mod's Nexus page:
|
|
|
|
// https://www.nexusmods.com/fallout4/mods/43458?tab=description
|
|
|
|
// Used for testing purposes only
|
|
|
|
var path = "TestData/WindowDisabled_CGPlayerHouseCube.dds".ToRelativePath().RelativeTo(KnownFolders.EntryPoint);
|
2022-12-28 16:21:58 +00:00
|
|
|
|
2023-01-21 19:36:12 +00:00
|
|
|
var baseState = await imageLoader.Load(path);
|
|
|
|
baseState.Height.Should().Be(128);
|
|
|
|
baseState.Width.Should().Be(128);
|
|
|
|
//baseState.Frames.Should().Be(6);
|
2022-12-28 16:21:58 +00:00
|
|
|
|
2023-01-21 19:36:12 +00:00
|
|
|
using var ms = new MemoryStream();
|
|
|
|
await using var ins = path.Open(FileMode.Open, FileAccess.Read, FileShare.Read);
|
|
|
|
await imageLoader.Recompress(ins, 128, 128, DXGI_FORMAT.BC1_UNORM, ms, CancellationToken.None, leaveOpen:true);
|
|
|
|
ms.Length.Should().Be(ins.Length);
|
|
|
|
}
|
2022-12-28 16:21:58 +00:00
|
|
|
}
|
|
|
|
|
2023-01-21 19:36:12 +00:00
|
|
|
public async ValueTask DisposeAsync()
|
|
|
|
{
|
|
|
|
await _tmp.DisposeAsync();
|
|
|
|
}
|
2021-09-27 12:42:46 +00:00
|
|
|
}
|