From 34b64ba266cf3fce7845ae8c37a71ae50fee22e6 Mon Sep 17 00:00:00 2001 From: Nominom Date: Thu, 18 Feb 2021 14:53:02 +0200 Subject: [PATCH] Fixed an issue with mipmap length calculation with maxmipmaps option set --- BCnEnc.Net/BCnEncoder.csproj | 8 +-- BCnEnc.Net/Shared/MipMapper.cs | 7 +- BCnEncTests/EncoderOptionsTests.cs | 71 +++++++++++++++++++ .../BCnEncoder.NET.ImageSharp.csproj | 6 +- 4 files changed, 84 insertions(+), 8 deletions(-) create mode 100644 BCnEncTests/EncoderOptionsTests.cs diff --git a/BCnEnc.Net/BCnEncoder.csproj b/BCnEnc.Net/BCnEncoder.csproj index daa7854..bf41bd8 100644 --- a/BCnEnc.Net/BCnEncoder.csproj +++ b/BCnEnc.Net/BCnEncoder.csproj @@ -1,4 +1,4 @@ - + netstandard2.1 @@ -12,7 +12,7 @@ MIT OR Unlicense false - 2.0.1 + 2.0.2 Nominom BCnEncoder.Net @@ -31,9 +31,9 @@ Supported formats are: git BCn BC BC1 BC2 BC3 BC4 BC5 BC7 BPTC RGTC S3TC DXT1 DXT3 DXT5 ktx dds texture compression encoding decoding decompression image gpu https://github.com/Nominom/BCnEncoder.NET - Removed ImageSharp dependency, added async api methods, added ATC and BGRA support, added new raw api methods, improved dds file handling and fixed some bugs. See the github page for more information about 2.0. + 2.0.0 - Removed ImageSharp dependency, added async api methods, added ATC and BGRA support, added new raw api methods, improved dds file handling and fixed some bugs. See the github page for more information about 2.0. -2.0.1 - fixed progress api +2.0.2 - fixed a bug in mipmap calculations diff --git a/BCnEnc.Net/Shared/MipMapper.cs b/BCnEnc.Net/Shared/MipMapper.cs index 9324405..c8cd7cb 100644 --- a/BCnEnc.Net/Shared/MipMapper.cs +++ b/BCnEnc.Net/Shared/MipMapper.cs @@ -82,11 +82,16 @@ public static int CalculateMipChainLength(int width, int height, int maxNumMipMa } var output = 0; - for (var mipLevel = 1; mipLevel < maxNumMipMaps; mipLevel++) + for (var mipLevel = 1; mipLevel <= maxNumMipMaps; mipLevel++) { var mipWidth = Math.Max(1, width >> mipLevel); var mipHeight = Math.Max(1, height >> mipLevel); + if (mipLevel == maxNumMipMaps) + { + return maxNumMipMaps; + } + if (mipWidth == 1 && mipHeight == 1) { output = mipLevel + 1; diff --git a/BCnEncTests/EncoderOptionsTests.cs b/BCnEncTests/EncoderOptionsTests.cs new file mode 100644 index 0000000..677fce4 --- /dev/null +++ b/BCnEncTests/EncoderOptionsTests.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using System.Text; +using BCnEncoder.Encoder; +using BCnEncTests.Support; +using Xunit; +using BCnEncoder.ImageSharp; + +namespace BCnEncTests +{ + public class EncoderOptionsTests + { + + [Theory] + [InlineData(1)] + [InlineData(2)] + [InlineData(5)] + [InlineData(10)] + public void MaxMipMaps(int requestedMipMaps) + { + var testImage = ImageLoader.TestBlur1; + var encoder = new BcEncoder() + { + OutputOptions = + { + GenerateMipMaps = true, + MaxMipMapLevel = requestedMipMaps + } + }; + + Assert.Equal(requestedMipMaps, encoder.CalculateNumberOfMipLevels(testImage)); + + var ktx = encoder.EncodeToKtx(testImage); + + Assert.Equal(requestedMipMaps, (int)ktx.header.NumberOfMipmapLevels); + Assert.Equal(requestedMipMaps, ktx.MipMaps.Count); + + var dds = encoder.EncodeToDds(testImage); + + Assert.Equal(requestedMipMaps, (int)dds.header.dwMipMapCount); + Assert.Equal(requestedMipMaps, dds.Faces[0].MipMaps.Length); + } + + [Fact] + [System.Diagnostics.CodeAnalysis.SuppressMessage("Assertions", "xUnit2013:Do not use equality check to check for collection size.", Justification = "")] + public void GenerateMipMaps() + { + var testImage = ImageLoader.TestBlur1; + const int requestedMipMaps = 1; + var encoder = new BcEncoder() + { + OutputOptions = + { + GenerateMipMaps = false + } + }; + + Assert.Equal(requestedMipMaps, encoder.CalculateNumberOfMipLevels(testImage)); + + var ktx = encoder.EncodeToKtx(testImage); + + Assert.Equal(requestedMipMaps, (int)ktx.header.NumberOfMipmapLevels); + Assert.Equal(requestedMipMaps, (int)ktx.MipMaps.Count); + + var dds = encoder.EncodeToDds(testImage); + + Assert.Equal(requestedMipMaps, (int)dds.header.dwMipMapCount); + Assert.Equal(requestedMipMaps, dds.Faces[0].MipMaps.Length); + } + } +} diff --git a/BCnEncoder.NET.ImageSharp/BCnEncoder.NET.ImageSharp.csproj b/BCnEncoder.NET.ImageSharp/BCnEncoder.NET.ImageSharp.csproj index 37c8e87..1d6c48d 100644 --- a/BCnEncoder.NET.ImageSharp/BCnEncoder.NET.ImageSharp.csproj +++ b/BCnEncoder.NET.ImageSharp/BCnEncoder.NET.ImageSharp.csproj @@ -17,8 +17,8 @@ https://github.com/Nominom/BCnEncoder.NET https://github.com/Nominom/BCnEncoder.NET git - Updated version - 1.0.1 + + 1.0.2 @@ -26,7 +26,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - +