Skip to content

Commit f6e1285

Browse files
committed
Fix zero level mipmaps in KTX2
1 parent a0d6f34 commit f6e1285

3 files changed

Lines changed: 39 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: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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.TextureFormats;
7+
8+
namespace SixLabors.ImageSharp.Textures.Tests.Formats.Ktx2;
9+
10+
[Trait("Format", "Ktx2")]
11+
public class Ktx2DecoderTests
12+
{
13+
private static readonly Ktx2Decoder Ktx2Decoder = new();
14+
15+
[Fact]
16+
[Description("Ensure that a single mipmap level does not result in an empty mipmap collection")]
17+
public void Ktx2Decoder_LevelCountZero_DecodesBaseLevelMipMap()
18+
{
19+
string path = Path.Combine(
20+
TestEnvironment.InputImagesDirectoryFullPath,
21+
"Ktx2",
22+
"Flat",
23+
"rgba32-srgb-mips.ktx2");
24+
25+
using FileStream fileStream = File.OpenRead(path);
26+
using Texture texture = Ktx2Decoder.DecodeTexture(Configuration.Default, fileStream);
27+
28+
FlatTexture flatTexture = texture as FlatTexture;
29+
Assert.NotNull(flatTexture);
30+
Assert.Single(flatTexture.MipMaps);
31+
Assert.Equal(256, flatTexture.MipMaps[0].GetImage().Width);
32+
Assert.Equal(256, flatTexture.MipMaps[0].GetImage().Height);
33+
}
34+
}
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)