Convert Figma logo to code with AI

KhronosGroup logoKTX-Software

KTX (Khronos Texture) Library and Tools

1,046
264
1,046
54

Top Related Projects

Basis Universal GPU Texture Codec

Tool suite for Texture and 3D Model Compression, Optimization and Analysis using CPUs, GPUs and APUs

DirectXTex texture processing library

The Arm ASTC Encoder, a compressor for the Adaptive Scalable Texture Compression data format.

Quick Overview

KTX-Software is a library and set of tools for creating, reading, and writing KTX (Khronos Texture) files. KTX is a container format for storing textures for OpenGL®, OpenGL ES™, and Vulkan® applications. The project provides both a C API and command-line tools for working with KTX files.

Pros

  • Cross-platform support (Windows, macOS, Linux, iOS, Android)
  • Supports both KTX and KTX2 formats
  • Includes command-line tools for texture conversion and manipulation
  • Integrates well with popular graphics APIs like OpenGL, Vulkan, and DirectX

Cons

  • Learning curve for developers unfamiliar with texture formats
  • Limited documentation for advanced use cases
  • Dependency on external libraries for some features
  • Potential performance overhead for real-time texture loading in some scenarios

Code Examples

Creating a KTX object from an image file:

ktxTexture* texture;
KTX_error_code result = ktxTexture_CreateFromNamedFile("image.png", KTX_TEXTURE_CREATE_LOAD_IMAGE_DATA_BIT, &texture);
if (result != KTX_SUCCESS) {
    // Handle error
}

Writing a KTX object to a file:

KTX_error_code result = ktxTexture_WriteToNamedFile(texture, "output.ktx");
if (result != KTX_SUCCESS) {
    // Handle error
}

Loading a KTX file and accessing its properties:

ktxTexture* texture;
KTX_error_code result = ktxTexture_CreateFromNamedFile("texture.ktx", KTX_TEXTURE_CREATE_LOAD_IMAGE_DATA_BIT, &texture);
if (result == KTX_SUCCESS) {
    ktx_uint32_t width = texture->baseWidth;
    ktx_uint32_t height = texture->baseHeight;
    ktx_uint32_t levels = texture->numLevels;
    // Use texture properties
}

Getting Started

  1. Clone the repository:

    git clone https://github.com/KhronosGroup/KTX-Software.git
    
  2. Build the library and tools:

    cd KTX-Software
    mkdir build && cd build
    cmake ..
    cmake --build .
    
  3. Include the library in your project:

    #include <ktx.h>
    
  4. Link against the built library (e.g., libktx.so on Linux, ktx.lib on Windows).

  5. Use the KTX API functions in your code, as shown in the code examples above.

Competitor Comparisons

Basis Universal GPU Texture Codec

Pros of basis_universal

  • Highly optimized compression algorithms for GPU textures
  • Supports a wider range of texture formats, including ASTC
  • More flexible encoding options for fine-tuning compression quality

Cons of basis_universal

  • Less integrated with the Khronos ecosystem
  • May require more manual setup and integration
  • Limited support for some advanced KTX features

Code Comparison

basis_universal:

basisu::basis_compressor_params params;
params.m_quality_level = 128;
params.m_compression_level = 1;
basisu::basis_compressor compressor;
compressor.init(params);

KTX-Software:

ktxTexture2* texture;
ktxTexture2_Create(&createInfo, KTX_TEXTURE_CREATE_ALLOC_STORAGE, &texture);
ktx_uint32_t iteratorIndex = 0;
ktxTexture2_IterateLoadLevelFaces(texture, &iteratorIndex, loadImageData, NULL);
ktxTexture2_WriteToMemory(texture, &ktxTextureData, &ktxTextureSize);

Both libraries offer efficient texture compression, but basis_universal provides more granular control over compression parameters, while KTX-Software integrates seamlessly with the KTX format and Khronos ecosystem. The choice between them depends on specific project requirements and target platforms.

Tool suite for Texture and 3D Model Compression, Optimization and Analysis using CPUs, GPUs and APUs

Pros of Compressonator

  • Supports a wider range of texture compression formats, including BC1-7, ASTC, and ETC
  • Offers advanced features like image analysis and quality metrics
  • Provides a GUI application for easier texture compression and analysis

Cons of Compressonator

  • Larger codebase and potentially more complex to integrate
  • May have a steeper learning curve due to its extensive feature set
  • Less focused on the KTX file format specifically

Code Comparison

KTX-Software:

ktxTexture* texture;
KTX_error_code result = ktxTexture_CreateFromNamedFile(
    "texture.ktx", KTX_TEXTURE_CREATE_LOAD_IMAGE_DATA_BIT, &texture
);

Compressonator:

CMP_Texture srcTexture;
CMP_LoadTexture("texture.dds", &srcTexture);
CMP_CompressTexture(&srcTexture, &destTexture, &compressOptions);

Both libraries offer texture loading and compression capabilities, but Compressonator provides more flexibility in terms of input/output formats and compression options. KTX-Software is more focused on the KTX format and offers a simpler API for working with KTX files specifically.

DirectXTex texture processing library

Pros of DirectXTex

  • Extensive support for DirectX-specific texture formats and operations
  • Robust toolset for texture processing, including mipmap generation and block compression
  • Well-integrated with Microsoft's DirectX ecosystem

Cons of DirectXTex

  • Limited cross-platform compatibility compared to KTX-Software
  • Focuses primarily on DirectX formats, potentially lacking support for some open standards
  • May have a steeper learning curve for developers not familiar with DirectX

Code Comparison

KTX-Software example:

ktxTexture* texture;
KTX_error_code result = ktxTexture_CreateFromNamedFile("texture.ktx", KTX_TEXTURE_CREATE_LOAD_IMAGE_DATA_BIT, &texture);

DirectXTex example:

DirectX::ScratchImage image;
HRESULT hr = DirectX::LoadFromDDSFile(L"texture.dds", DirectX::DDS_FLAGS_NONE, nullptr, image);

Both libraries provide functions for loading texture files, but DirectXTex is more tightly integrated with DirectX types and conventions, while KTX-Software uses a more generic C-style API.

The Arm ASTC Encoder, a compressor for the Adaptive Scalable Texture Compression data format.

Pros of astc-encoder

  • Specialized for ASTC texture compression, offering high-quality compression for a wide range of image types
  • Provides advanced encoding options and fine-tuned control over compression parameters
  • Actively maintained with regular updates and optimizations

Cons of astc-encoder

  • Limited to ASTC format, while KTX-Software supports multiple texture formats
  • Steeper learning curve due to more complex command-line options and parameters
  • Lacks built-in support for container formats like KTX

Code Comparison

astc-encoder:

astcenc_config config;
astcenc_context* context;
astcenc_image image;
uint8_t* compressed_data;

KTX-Software:

ktxTexture* texture;
KTX_error_code result;
ktx_uint8_t* ktx_data;
ktx_size_t ktx_size;

Both libraries offer low-level APIs for texture compression and manipulation, but KTX-Software provides a more comprehensive solution for working with various texture formats and container files. astc-encoder excels in ASTC-specific compression tasks, offering more granular control over the encoding process. The choice between the two depends on the specific requirements of the project, with KTX-Software being more versatile and astc-encoder being more specialized for ASTC compression.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

The Official Khronos KTX Software Repository

GNU/Linux, iOS, macOS & wasmWindowsAndroidMingw
Build StatusBuild statusKTX-Software CIKTX-Software CI

This is the official home of the source code for the Khronos KTX library and tools.

KTX (Khronos Texture) is a lightweight container for textures for OpenGL®, Vulkan® and other GPU APIs. KTX files contain all the parameters needed for texture loading. A single file can contain anything from a simple base-level 2D texture through to a cubemap array texture with mipmaps. Contained textures can be in a Basis Universal format, in any of the block-compressed formats supported by OpenGL family and Vulkan APIs and extensions or in an uncompressed single-plane format. Basis Universal currently encompasses two formats that can be quickly transcoded to any GPU-supported format: LZ/ETC1S, which combines block-compression and supercompression, and UASTC, a block-compressed format. Formats other than LZ/ETC1S can be supercompressed with Zstd and ZLIB.

Download KTX Software Releases to get binary packages of the tools, library and development headers described below. The Releases page also has packages with the Javascript wrappers and .wasm binaries.

See the Doxygen generated live documentation for API and tool usage information.

The software consists of: (links are to source folders in the KhronosGroup repo)

  • libktx - a small library of functions for writing and reading KTX files, and instantiating OpenGL®, OpenGL ES™️ and Vulkan® textures from them. lib
  • libktx.{js,wasm} - Web assembly version of libktx and Javascript wrapper. interface/js_binding
  • msc_basis_transcoder.{js,wasm} - Web assembly transcoder and Javascript wrapper for Basis Universal formats. For use with KTX parsers written in Javascript. interface/js_binding
  • libktx.jar, libktx-jni - Java wrapper and native interface library. interface/java_binding
  • ktx - a generic command line tool for managing KTX2 files with subcommands.tools/ktx
    • ktx compare - Compare two KTX2 files
    • ktx create - Create a KTX2 file from various input files
    • ktx deflate - Deflate a KTX2 file with zstd or ZLIB
    • ktx extract - Export selected images from a KTX2 file
    • ktx encode - Encode a KTX2 file
    • ktx transcode - Transcode a KTX2 file
    • ktx info - Prints information about a KTX2 file
    • ktx validate - Validate a KTX2 file
    • ktx help - Display help information about the ktx tools
  • ktx2check - a tool for validating KTX Version 2 format files. tools/ktx2check
  • ktx2ktx2 - a tool for converting a KTX Version 1 file to a KTX Version 2 file. tools/ktx2ktx2
  • ktxinfo - a tool to display information about a KTX file in human readable form. tools/ktxinfo
  • ktxsc - a tool to supercompress a KTX Version 2 file that contains uncompressed images.tools/ktxsc
  • pyktx - Python wrapper
  • toktx - a tool to create KTX files from PNG, Netpbm or JPEG format images. It supports mipmap generation, encoding to Basis Universal formats and Zstd supercompression.tools/toktx

See CONTRIBUTING for information about contributing.

See LICENSE for information about licensing.

See BUILDING for information about building the code.

If you need help with using the KTX library or KTX tools, please use GitHub Discussions. To report problems use GitHub issues.

IMPORTANT: you must install the Git LFS command line extension in order to fully checkout this repository after cloning. You need at least version 1.1. If you did not have Git LFS installed at first checkout then, after installing it, you must run

git lfs checkout

KTX-Software-CTS - Conformance Test Suite

The tests and test files for the generic command line ktx tool can be found in a separate CTS Repository. To save space and bandwidth this repository is included with git submodule and by default it is not required for building the libraries or the tools. For more information about building, running and extending the CTS tests see BUILDING and CTS README.

$Date$ keyword expansion

A few files have $Date$ keywords. If you care about having the proper dates shown or will be generating the documentation or preparing distribution archives, you must follow the instructions below.

$Date$ keywords are expanded via smudge & clean filters. To install the filters, issue the following commands in the root of your clone.

On Unix (Linux, Mac OS X, etc.) platforms and Windows using Git for Windows' Git Bash or Cygwin's bash terminal:

./install-gitconfig.sh
./scripts/smudge_date.sh

On Windows PowerShell (requires git.exe in a directory on your %PATH%):

install-gitconfig.ps1
./scripts/smudge_date.ps1

The first command adds an [include] of the repo's .gitconfig to the local git config file .git/config, i.e. the one in your clone of the repo. .gitconfig contains the config of the "keyworder" filter. The script in the second command forces a new checkout of the affected files to smudge them with their last modified date. This is unnecessary if you plan to edit these files.

Useful Tools

scripts/gk

For finding strings within the KTX-Software source. Type scripts/gk -h for help. gk avoids looking in any build directories, .git, external or tests/cts.

scripts/ktx-compare-git

Wrapper that allows use of ktx compare when using git diff on KTX2 files. Together with this, .gitconfig now includes a ktx-compare diff command. Those wishing to use this must run, or have run, install-gitconfig.{ps1,sh} as described above for keyword expansion so that .gitconfig is included by your local .git/config.

You need to have the ktx command installed in a directory on your $PATH.

You need to add the line

*.ktx2 binary diff=ktx-compare

to your repo clone's .git/info/attributes. This is not included in the repo's .gitattributes because not everyone will have the ktx command installed nor have .gitconfig included by .git/config.

NOTE: This line in a user-global or system-global Git attributes file will not work because those are lower priority than .gitattributes so are read first and .gitattributes already has an entry for *.ktx2, indicating binary, which overrides anything from the global files.

To set up your tests/cts submodule to use this, copy the ktx-compare diff command to .git/modules/tests/cts/config. Depending on when you set up the submodule and when you ran install-gitconfig.sh, it may already be there. Add the attribute line to .git/modules/tests/cts/info/attributes.

We will be happy to accept a PR to add a .ps1 equivalent script.