Documentation
¶
Overview ¶
Package ktx2 provides a pure-Go KTX2 v2.0 texture container encoder, decoder, supercompression decoder (BasisLZ, Zstandard, ZLIB), mipmap chain generator, Basis Universal ETC1S and UASTC 4x4 codecs, and block texture transcoder (BC1, BC3, BC7, ETC2 RGB8/RGBA8, ASTC 4x4, RGBA8).
Index ¶
- Constants
- Variables
- func DecodeHeader(data []byte) (Header, []LevelIndex, error)
- func DecodeKVD(kvdBytes []byte) (map[string][]byte, error)
- func DecodeRGBA(data []byte, level int) (*image.NRGBA, error)
- func Encode(img image.Image, opts Options) ([]byte, error)
- func EncodeFile(inPath, outPath string, opts Options) error
- func EncodeWriter(w io.Writer, img image.Image, opts Options) error
- func GenerateMipmaps(src image.Image, srgb bool) []*image.NRGBA
- func MipDimensions(baseW, baseH, level int) (int, int)
- func MipLevelCount(width, height int) int
- func ReadLevel(data []byte, level int) (raw []byte, width, height int, err error)
- func ToNRGBA(src image.Image) *image.NRGBA
- func Validate(data []byte) error
- type DFD
- type DFDSample
- type Format
- type Header
- type Level
- type LevelIndex
- type Options
- type Texture
Examples ¶
Constants ¶
const ( VkFormatUndefined uint32 = 0 VkFormatR8G8B8A8Unorm uint32 = 37 VkFormatR8G8B8A8SRGB uint32 = 43 VkFormatBC1RGBAUnormBlock uint32 = 133 VkFormatBC1RGBASRGBBlock uint32 = 134 VkFormatBC3UnormBlock uint32 = 137 VkFormatBC3SRGBBlock uint32 = 138 VkFormatBC7UnormBlock uint32 = 145 VkFormatBC7SRGBBlock uint32 = 146 VkFormatETC2R8G8B8UnormBlk uint32 = 147 VkFormatETC2R8G8B8SRGBBlk uint32 = 148 VkFormatETC2R8G8B8A8UnormBlk uint32 = 151 VkFormatETC2R8G8B8A8SRGBBlk uint32 = 152 VkFormatASTC4x4UnormBlock uint32 = 157 VkFormatASTC4x4SRGBBlock uint32 = 158 )
Vulkan VkFormat constants used by this package.
const ( DFDVendorKhronos uint32 = 0 DFDDescriptorBasicFormat uint32 = 0 DFDVersionNumber uint32 = 2 DFDModelRGBSDA uint8 = 1 DFDModelBC1A uint8 = 128 DFDModelBC3 uint8 = 130 DFDModelBC7 uint8 = 134 DFDModelETC2 uint8 = 161 DFDModelASTC uint8 = 162 DFDModelETC1S uint8 = 163 DFDModelUASTC uint8 = 166 DFDPrimariesBT709 uint8 = 1 DFDTransferLinear uint8 = 1 DFDTransferSRGB uint8 = 2 DFDChannelRGBSDARed uint8 = 0 DFDChannelRGBSDAGreen uint8 = 1 DFDChannelRGBSDABlue uint8 = 2 DFDChannelRGBSDAAlpha uint8 = 15 DFDChannelBC1AColor uint8 = 0 DFDChannelBC3Color uint8 = 0 DFDChannelBC3Alpha uint8 = 15 DFDChannelBC7Color uint8 = 0 DFDChannelETC2Color uint8 = 0 DFDChannelETC2Alpha uint8 = 15 DFDChannelASTCData uint8 = 0 DFDChannelETC1SRGB uint8 = 0 DFDChannelETC1SRRR uint8 = 3 DFDChannelETC1SGGG uint8 = 4 DFDChannelETC1SAAA uint8 = 15 DFDChannelUASTCRGB uint8 = 0 DFDChannelUASTCRGBA uint8 = 3 DFDChannelUASTCRRR uint8 = 4 DFDChannelUASTCRRRG uint8 = 5 SupercompressionNone uint32 = 0 SupercompressionBasisLZ uint32 = 1 SupercompressionZstandard uint32 = 2 SupercompressionZLIB uint32 = 3 )
Khronos Data Format Descriptor (DFD) v1.3 constants.
const HeaderSize = 80
HeaderSize is the fixed size in bytes of the KTX2 identifier + header + index section.
const LevelIndexEntrySize = 24
LevelIndexEntrySize is the byte size of one entry in the KTX2 level index array.
Variables ¶
var Identifier = [12]byte{0xAB, 0x4B, 0x54, 0x58, 0x20, 0x32, 0x30, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A}
Identifier is the 12-byte KTX2 v2.0 magic file signature: "\xABKTX 20\xBB\r\n\x1A\n".
Functions ¶
func DecodeHeader ¶
func DecodeHeader(data []byte) (Header, []LevelIndex, error)
DecodeHeader parses the KTX2 identifier, header, and level index array.
func DecodeKVD ¶
DecodeKVD parses the Key/Value Data section of a KTX2 file and verifies lexicographical key order.
func DecodeRGBA ¶
DecodeRGBA decodes the specified mip level (0 = base) of a KTX2 texture into *image.NRGBA. Supports RGBA8, BC1, BC3, BC7, ETC2 RGB8/RGBA8, ASTC 4x4, Basis Universal UASTC 4x4, and BasisLZ/ETC1S with uncompressed, BasisLZ, Zstandard, or ZLIB supercompression.
func Encode ¶
Encode encodes img into a spec-compliant KTX2 v2.0 byte slice.
Example ¶
Encode writes an image as a KTX2 texture, optionally block-compressed and with a full mipmap chain; DecodeHeader and DecodeRGBA read it back.
package main
import (
"fmt"
"image"
"image/color"
"github.com/nsgo-dev/game/pkg/ktx2"
)
func main() {
img := image.NewNRGBA(image.Rect(0, 0, 16, 8))
for y := range 8 {
for x := range 16 {
img.SetNRGBA(x, y, color.NRGBA{R: uint8(x * 16), G: uint8(y * 32), A: 255})
}
}
data, err := ktx2.Encode(img, ktx2.Options{Format: ktx2.FormatRGBA8, Mipmaps: true})
if err != nil {
fmt.Println(err)
return
}
h, levels, err := ktx2.DecodeHeader(data)
fmt.Println(err, h.PixelWidth, h.PixelHeight, h.LevelCount, len(levels))
back, _ := ktx2.DecodeRGBA(data, 0)
fmt.Println(back.NRGBAAt(3, 2), ktx2.Validate(data))
}
Output: <nil> 16 8 5 5 {48 64 0 255} <nil>
func EncodeFile ¶
EncodeFile reads a PNG or JPEG image from inPath and writes a KTX2 texture to outPath.
func EncodeWriter ¶
EncodeWriter encodes img as KTX2 and writes it to w.
func GenerateMipmaps ¶
GenerateMipmaps generates a complete mipmap chain down to 1x1 starting from src. When srgb is true, RGB channels are converted to linear light prior to filtering and converted back to sRGB afterward.
func MipDimensions ¶
MipDimensions returns the width and height of mip level (0-based) for a base texture size.
Example ¶
MipLevelCount and MipDimensions give the size of each level in a mipmap chain.
package main
import (
"fmt"
"github.com/nsgo-dev/game/pkg/ktx2"
)
func main() {
n := ktx2.MipLevelCount(64, 16)
for level := range n {
w, h := ktx2.MipDimensions(64, 16, level)
fmt.Print(w, "x", h, " ")
}
fmt.Println()
}
Output: 64x16 32x8 16x4 8x2 4x1 2x1 1x1
func MipLevelCount ¶
MipLevelCount returns the total number of mipmap levels from (width, height) down to 1x1.
func ReadLevel ¶
ReadLevel returns the raw (on-disk) bytes and pixel dimensions for mip level (0 = base level).
Types ¶
type DFD ¶
type DFD struct {
TotalSize uint32
VendorID uint32
DescriptorType uint32
VersionNumber uint32
DescriptorBlockSize uint32
ColorModel uint8
ColorPrimaries uint8
TransferFunction uint8
Flags uint8
TexelBlockDimensions [4]uint8 // stored as dim - 1 per Khronos DFD v1.3
BytesPlane [8]uint8
Samples []DFDSample
}
DFD represents a parsed KTX2 Basic Data Format Descriptor block.
type DFDSample ¶
type DFDSample struct {
BitOffset uint16
BitLength uint8 // stored as bit_length - 1 per Khronos DFD v1.3
ChannelType uint8
SamplePosition [4]uint8
SampleLower uint32
SampleUpper uint32
}
DFDSample describes one sample block inside a KTX2 Basic Data Format Descriptor.
type Format ¶
type Format string
Format identifies a supported KTX2 pixel, universal, or block-compressed texture format.
func FormatFromHeaderDFD ¶
FormatFromHeaderDFD resolves the texture format, sRGB transfer flag, and alpha presence from a KTX2 header and parsed Data Format Descriptor, including Basis Universal UASTC and ETC1S containers that set Header.VkFormat == 0.
func FormatFromVk ¶
FormatFromVk maps a supported Vulkan VkFormat ID back to (Format, srgb, ok). For VkFormatUndefined (0), use FormatFromHeaderDFD to resolve Basis Universal UASTC or ETC1S from the Data Format Descriptor.
func ParseFormat ¶
ParseFormat parses a format name as game tex's -format flag and an image sidecar spell it (rgba8, bc1/dxt1, bc3/dxt5 or etc2), the formats the asset pipeline writes. The other formats this package encodes and decodes, such as FormatBC7, FormatETC2RGBA, FormatASTC4x4 and FormatUASTC, are set through Options.Format from Go.
func (Format) BlockBytes ¶
BlockBytes returns the byte size of one texel block (4 for RGBA8, 8 for BC1/ETC2, 16 for BC3/BC7/ETC2RGBA/ASTC4x4/UASTC).
func (Format) BlockDim ¶
BlockDim returns the square texel block dimension (4 for block formats, 1 for RGBA8).
func (Format) IsBlockCompressed ¶
IsBlockCompressed reports whether f stores 4x4 texel blocks rather than uncompressed pixels.
func (Format) LevelByteSize ¶
LevelByteSize returns the exact uncompressed byte size of a w x h mip level encoded in f.
type Header ¶
type Header struct {
VkFormat uint32
TypeSize uint32
PixelWidth uint32
PixelHeight uint32
PixelDepth uint32
LayerCount uint32
FaceCount uint32
LevelCount uint32
SupercompressionScheme uint32
DFDByteOffset uint32
DFDByteLength uint32
KVDByteOffset uint32
KVDByteLength uint32
SGDByteOffset uint64
SGDByteLength uint64
}
Header represents the 80-byte KTX2 v2.0 file header (including index offsets).
type LevelIndex ¶
LevelIndex describes one mip level entry in the KTX2 level index array.
type Options ¶
type Options struct {
// Format selects pixel, universal, or block compression (default FormatRGBA8).
Format Format
// Mipmaps generates a complete mipmap chain down to 1x1 when true.
Mipmaps bool
// SRGB encodes using the corresponding Vulkan sRGB format, sRGB DFD transfer function, and sRGB-aware linear-light mipmap filtering.
SRGB bool
// Supercompression selects an optional KTX2 supercompression scheme (SupercompressionNone default, or SupercompressionZLIB).
Supercompression uint32
// KVD adds optional key/value metadata pairs to the KTX2 Key/Value Data section. Standard keys KTXorientation and KTXwriter are populated automatically if absent.
KVD map[string]string
}
Options configures KTX2 texture encoding.
type Texture ¶
type Texture struct {
Header Header
DFD DFD
KVD map[string][]byte
Format Format
SRGB bool
HasAlpha bool
Width int
Height int
Levels []Level
SGD []byte
// contains filtered or unexported fields
}
Texture holds a fully parsed and validated 2D KTX2 v2.0 container.
func Decode ¶
Decode parses, validates, and de-supercompresses a 2D KTX2 v2.0 container. Supports SupercompressionNone (0), SupercompressionBasisLZ (1), SupercompressionZstandard (2), and SupercompressionZLIB (3) across RGBA8, BC1, BC3, BC7, ETC2 RGB8/RGBA8, ASTC 4x4, Basis Universal UASTC 4x4, and BasisLZ/ETC1S.
func (*Texture) DecodeLevelRGBA ¶
DecodeLevelRGBA decodes mip level (0 = base) of t into *image.NRGBA.