Skip to content

Commit a2926ed

Browse files
authored
Merge pull request #42 from Erik-White/bugfix/ktx2-missing-mips
KTX2 textures with single MipMap fail to decode
2 parents 22b2090 + 05e5e20 commit a2926ed

4 files changed

Lines changed: 42 additions & 1 deletion

File tree

src/ImageSharp.Textures/Formats/Ktx2/Ktx2DecoderCore.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ public Texture DecodeTexture(Stream stream)
6969
int width = (int)this.ktxHeader.PixelWidth;
7070
int height = (int)this.ktxHeader.PixelHeight;
7171

72-
var levelIndices = new LevelIndex[this.ktxHeader.LevelCount];
72+
// Zero levels means only the base level is present, so we must read at least 1.
73+
LevelIndex[] levelIndices = new LevelIndex[Math.Max(1, this.ktxHeader.LevelCount)];
7374
for (int i = 0; i < levelIndices.Length; i++)
7475
{
7576
stream.Read(this.buffer, 0, 24);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Six Labors Split License.
3+
4+
using System.ComponentModel;
5+
using SixLabors.ImageSharp.Textures.Formats.Ktx2;
6+
using SixLabors.ImageSharp.Textures.Tests.Enums;
7+
using SixLabors.ImageSharp.Textures.Tests.TestUtilities.Attributes;
8+
using SixLabors.ImageSharp.Textures.Tests.TestUtilities.TextureProviders;
9+
using SixLabors.ImageSharp.Textures.TextureFormats;
10+
11+
namespace SixLabors.ImageSharp.Textures.Tests.Formats.Ktx2;
12+
13+
[Trait("Format", "Ktx2")]
14+
public class Ktx2DecoderFlatTests
15+
{
16+
private static readonly Ktx2Decoder Ktx2Decoder = new();
17+
18+
[Theory]
19+
[Description("Ensure that a single mipmap level does not result in an empty mipmap collection")]
20+
[WithFile(TestTextureFormat.Ktx2, TestTextureType.Flat, TestTextureTool.ToKtx, TestImages.Ktx2.Rgba32SrgbMips)]
21+
public void Ktx2Decoder_LevelCountZero_DecodesBaseLevelMipMap(TestTextureProvider provider)
22+
{
23+
using Texture texture = provider.GetTexture(Ktx2Decoder);
24+
provider.SaveTextures(texture);
25+
26+
FlatTexture flatTexture = texture as FlatTexture;
27+
Assert.NotNull(flatTexture);
28+
Assert.Single(flatTexture.MipMaps);
29+
Assert.Equal(256, flatTexture.MipMaps[0].GetImage().Width);
30+
Assert.Equal(256, flatTexture.MipMaps[0].GetImage().Height);
31+
}
32+
}

tests/ImageSharp.Textures.Tests/TestImages.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,10 @@ public static class Ktx
1212
{
1313
public const string Rgba = "rgba8888.ktx";
1414
}
15+
16+
public static class Ktx2
17+
{
18+
public const string Rgba32SrgbMips = "rgba32-srgb-mips.ktx2";
19+
}
1520
}
1621
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version https://git-lfs.github.com/spec/v1
2+
oid sha256:90846eff319fdfd03121b3e0300fc4106ddcc2a4df881598cca9978e04710004
3+
size 393468

0 commit comments

Comments
 (0)