Skip to content

Commit

Permalink
Fixed an issue with mipmap length calculation with maxmipmaps option set
Browse files Browse the repository at this point in the history
  • Loading branch information
Nominom committed Feb 18, 2021
1 parent 8490e72 commit 34b64ba
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 8 deletions.
8 changes: 4 additions & 4 deletions BCnEnc.Net/BCnEncoder.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
Expand All @@ -12,7 +12,7 @@
<PackageLicenseExpression>MIT OR Unlicense</PackageLicenseExpression>
<Copyright />
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<Version>2.0.1</Version>
<Version>2.0.2</Version>
<Authors>Nominom</Authors>
<Company />
<Product>BCnEncoder.Net</Product>
Expand All @@ -31,9 +31,9 @@ Supported formats are:
<RepositoryType>git</RepositoryType>
<PackageTags>BCn BC BC1 BC2 BC3 BC4 BC5 BC7 BPTC RGTC S3TC DXT1 DXT3 DXT5 ktx dds texture compression encoding decoding decompression image gpu</PackageTags>
<PackageProjectUrl>https://github.com/Nominom/BCnEncoder.NET</PackageProjectUrl>
<PackageReleaseNotes>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.
<PackageReleaseNotes>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</PackageReleaseNotes>
2.0.2 - fixed a bug in mipmap calculations</PackageReleaseNotes>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
Expand Down
7 changes: 6 additions & 1 deletion BCnEnc.Net/Shared/MipMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
71 changes: 71 additions & 0 deletions BCnEncTests/EncoderOptionsTests.cs
Original file line number Diff line number Diff line change
@@ -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 = "<Pending>")]
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);
}
}
}
6 changes: 3 additions & 3 deletions BCnEncoder.NET.ImageSharp/BCnEncoder.NET.ImageSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@
<PackageProjectUrl>https://github.com/Nominom/BCnEncoder.NET</PackageProjectUrl>
<RepositoryUrl>https://github.com/Nominom/BCnEncoder.NET</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageReleaseNotes>Updated version</PackageReleaseNotes>
<Version>1.0.1</Version>
<PackageReleaseNotes></PackageReleaseNotes>
<Version>1.0.2</Version>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.2" />
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.3" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 34b64ba

Please sign in to comment.