Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/ImageSharp.Textures/Formats/Ktx2/Ktx2DecoderCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public Texture DecodeTexture(Stream stream)
int width = (int)this.ktxHeader.PixelWidth;
int height = (int)this.ktxHeader.PixelHeight;

var levelIndices = new LevelIndex[this.ktxHeader.LevelCount];
// Zero levels means only the base level is present, so we must read at least 1.
LevelIndex[] levelIndices = new LevelIndex[Math.Max(1, this.ktxHeader.LevelCount)];
for (int i = 0; i < levelIndices.Length; i++)
{
stream.Read(this.buffer, 0, 24);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.

using System.ComponentModel;
using SixLabors.ImageSharp.Textures.Formats.Ktx2;
using SixLabors.ImageSharp.Textures.Tests.Enums;
using SixLabors.ImageSharp.Textures.Tests.TestUtilities.Attributes;
using SixLabors.ImageSharp.Textures.Tests.TestUtilities.TextureProviders;
using SixLabors.ImageSharp.Textures.TextureFormats;

namespace SixLabors.ImageSharp.Textures.Tests.Formats.Ktx2;

[Trait("Format", "Ktx2")]
public class Ktx2DecoderFlatTests
{
private static readonly Ktx2Decoder Ktx2Decoder = new();

[Theory]
[Description("Ensure that a single mipmap level does not result in an empty mipmap collection")]
[WithFile(TestTextureFormat.Ktx2, TestTextureType.Flat, TestTextureTool.ToKtx, TestImages.Ktx2.Rgba32SrgbMips)]
public void Ktx2Decoder_LevelCountZero_DecodesBaseLevelMipMap(TestTextureProvider provider)
{
using Texture texture = provider.GetTexture(Ktx2Decoder);
provider.SaveTextures(texture);

FlatTexture flatTexture = texture as FlatTexture;
Assert.NotNull(flatTexture);
Assert.Single(flatTexture.MipMaps);
Assert.Equal(256, flatTexture.MipMaps[0].GetImage().Width);
Assert.Equal(256, flatTexture.MipMaps[0].GetImage().Height);
}
}
5 changes: 5 additions & 0 deletions tests/ImageSharp.Textures.Tests/TestImages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,10 @@ public static class Ktx
{
public const string Rgba = "rgba8888.ktx";
}

public static class Ktx2
{
public const string Rgba32SrgbMips = "rgba32-srgb-mips.ktx2";
}
}
}
3 changes: 3 additions & 0 deletions tests/Images/Input/Ktx2/rgba32-srgb-mips.ktx2
Git LFS file not shown
Loading