Class Ktx2Format
KTX 2.0, the Khronos texture container for Vulkan, glTF and the web: a header naming the Vulkan format, a level index, a data format descriptor with the colour space, key/value data, then the mip levels smallest first, each optionally supercompressed with Zstandard or zlib. Load gives mip 0 of the first surface, turned upright and swizzled as the file says; LoadAll and KtxReaderOptions reach the others. Uncompressed, BC1 to BC7, ETC2, EAC, 2D ASTC (LDR and HDR) and Basis Universal (ETC1S, UASTC LDR and HDR 4 × 4) textures read; 3D ASTC, PVRTC and the newer Basis Universal families (UASTC HDR 6 × 6, XUASTC, XUBC7) are refused. Save and SaveAll write every one of those but ASTC HDR and UASTC HDR, with mips, arrays, cube maps and supercompression (Ktx2WriterOptions). Registered with WithKtx2(EgamiContext).
public static class Ktx2Format
- Inheritance
-
Ktx2Format
- Inherited Members
Examples
// KTX 1.1 (.ktx) and KTX 2.0 (.ktx2) are two formats; WithExtendedFormats() registers both.
EgamiContext context = EgamiContext.Default.WithKtx().WithKtx2();
// Load gives mip 0 of the first surface, upright, in the format's natural pixel type (Rgba8 for RGBA8 and BC7).
using Raster texture = Raster.Load("texture.ktx2", context);
texture.Save("texture.png");
// Probe reads the headers alone: the surfaces at mip 0 are its images.
RasterInfo info = Raster.Probe("sky.ktx2", context);
Console.WriteLine($"{info.Width}×{info.Height}, {info.ImageCount} surface(s)");
// With no options: the nearest uncompressed format (R8G8B8A8_SRGB for 8-bit colour) and the full mip chain.
using (FileStream output = File.Create("duck.ktx2"))
{
image.Save(output, Ktx2Format.Info);
}
// A GPU block format: BC7 for desktop, ASTC or ETC2 for mobile; effort runs 0 (fastest) to 6 (best).
using (FileStream output = File.Create("duck-astc.ktx2"))
{
image.Save(output, Ktx2Format.Info, new Ktx2WriterOptions { Format = KtxTextureFormat.Astc6x6, Effort = 3 });
}
// Basis Universal, which a viewer transcodes to whatever its GPU reads: UASTC with RDO and Zstandard (glTF's usual
// pick), or ETC1S for the smallest files.
using (FileStream output = File.Create("duck-uastc.ktx2"))
{
image.Save(output, Ktx2Format.Info, new Ktx2WriterOptions
{
Format = KtxTextureFormat.Uastc,
UastcRdo = 2,
Supercompression = KtxSupercompression.Zstandard,
});
}
using (FileStream output = File.Create("duck-etc1s.ktx2"))
{
image.Save(output, Ktx2Format.Info, new Ktx2WriterOptions { Format = KtxTextureFormat.Etc1s, BasisQuality = 128 });
}
// Six faces into a cube map; SaveAll without Cube writes a texture array.
using RasterList faces = Raster.LoadAll("sky.ktx2", context);
using (FileStream output = File.Create("sky-bc1.ktx2"))
{
Raster.SaveAll(faces, output, Ktx2Format.Info, new Ktx2WriterOptions { Cube = true, Format = KtxTextureFormat.Bc1 });
}
Properties
Info
Gets the format's registration: name, MIME type, extension, signature, reader and writer.
public static FormatInfo Info { get; }