Single header KTX/DDS reader

Overview

dds-ktx: Portable single header DDS/KTX reader for C/C++

@septag

  • Parses from memory blob. No allocations
  • No dependencies
  • Single-header for easy integration
  • Overridable libc functions

Build Example (ctexview)

ctexview is a tiny ddx-ktx viewer that can be built on linux/mac and windows. To build it just compile the single file with your compiler.

Windows:

cl ctexview.c /O2

Linuxx

gcc ctexview.c -O2 -lGL -ldl -lX11 -lXi -lXcursor -lm -o ctexview

MacOS

clang -framework Foundation -framework CoreServices -framework CoreFoundation -O2 -fobjc-arc -x objective-c -fmodules -x objective-c ctexview.c -o ./ctexview

to view images just provide the image path as an argument:

ctexview [dds_or_ktx_image_file_path]

Used open-source libraries for app creation/graphics: Sokol

Keys:

  • UP/DOWN: change current mipmap
  • Apostrophe: change text color
  • F: Next cube-map face
  • R: Toggle Red channel
  • G: Toggle Green channel
  • B: Toggle Blue channel
  • A: Toggle Alpha channel

Usage

In this example, a simple 2D texture is parsed and created using OpenGL

#define DDSKTX_IMPLEMENT
#include "dds-ktx.h"

int size;
void* dds_data = load_file("test.dds", &size);
assert(dds_data);
ddsktx_texture_info tc = {0};
GLuint tex = 0;
if (ddsktx_parse(&tc, dds_data, size, NULL)) {
    assert(tc.depth == 1);
    assert(!(tc.flags & STC_TEXTURE_FLAG_CUBEMAP));
    assert(tc.num_layers == 1);

    //Create GPU texture from tc data
    glGenTextures(1, &tex);
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(img->gl_target, tex);

    for (int mip = 0; mip < tc->num_mips; mip++) {
        ddsktx_sub_data sub_data;
        ddsktx_get_sub(&tc, &sub_data, dds_data, size, 0, 0, mip);
        // Fill/Set texture sub resource data (mips in this case)
        if (ddsktx_format_compressed(tc.format))
            glCompressedTexImage2D(..);
        else
            glTexImage2D(..);
    }

    // Now we can delete file data
    free(dds_data);
}

Links

TODO

  • Write KTX/DDS
  • Read KTX metadata. currently it just stores the offset/size to the metadata block

Others

  • stb_image - Single header library that loads images (.png, .jpg, .bmp, etc)
  • bimg - Extensive C++ image library

NOTE: Many parts of the code is taken from bimg library.

License (BSD 2-clause)

Copyright 2018 Sepehr Taghdisian. All rights reserved.

https://github.com/septag/dds-ktx

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

   1. Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.

   2. Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY COPYRIGHT HOLDER ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
EVENT SHALL COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
You might also like...
A header-only C-like shading language compiler that writes Metal, HLSL, GLSL

GPUC A generic shading language compiler that writes metal, HLSL, and GLSL GPUC is a work in progress, not ready for prime time. The primary motivatio

Converts common image formats (PNG, JPG, etc.) to GPU-native compressed (BCn, ETC, ASTC) in KTX containers.

Converts common image formats (PNG, JPG, etc.) to GPU-native compressed (BCn, ETC, ASTC) in KTX containers.

Demonstrates basic advantages of integrating the Data Distribution Service (DDS) and Time-Sensitive Networking (TSN) Ethernet.
Demonstrates basic advantages of integrating the Data Distribution Service (DDS) and Time-Sensitive Networking (TSN) Ethernet.

ROS2-DDS-TSN integration demo This repository demonstrates basic advantages of integrating the Data Distribution Service (DDS) and Time-Sensitive Netw

Direct Digital Synthesis DDS with microcontroller
Direct Digital Synthesis DDS with microcontroller

Direct Digital Synthesis DDS with microcontroller DDS is a cool thing. To understand how it works is even cooler. So I wrote a little tutorial about D

Simple DDS Generator base on AD9833 with STM32 microcontroller.
Simple DDS Generator base on AD9833 with STM32 microcontroller.

AD9833-DDS-Generator Description A simple signal generator based on the AD9833 chip. The project is built on the STM32F401 microcontroller using a 1.8

An image and texture viewer for tga, png, apng, exr, dds, gif, hdr, jpg, tif, ico, webp, and bmp files
An image and texture viewer for tga, png, apng, exr, dds, gif, hdr, jpg, tif, ico, webp, and bmp files

An image and texture viewer for tga, png, apng, exr, dds, gif, hdr, jpg, tif, ico, webp, and bmp files. Uses Dear ImGui, OpenGL, and Tacent. Useful for game devs as it displays information like the presence of an alpha channel and querying specific pixels for their colour.

A single file, single function, header to make notifications on the PS4 easier

Notifi Synopsis Adds a single function notifi(). It functions like printf however the first arg is the image to use (NULL and any invalid input should

Single-header single-function C/C++ immediate-mode camera for your graphics demos
Single-header single-function C/C++ immediate-mode camera for your graphics demos

Single-header single-function C/C++ immediate-mode camera for your graphics demos

Convert YouTube Subscriptions JSON into RSS Reader Compatible OPML

OPMLify Convert YouTube Subscriptions JSON into RSS Reader Compatible OPML Brief Overview OPMLify allows you to import your YouTube Subscriptions to a

An unofficial Realtek PCIe-based card reader driver for macOS

Realtek PCIe Card Reader Driver for macOS Unleash the full potential of your SDXC UHS-I cards Introduction An unofficial macOS kernel extension for Re

An unofficial Realtek PCIe/USB-based SD card reader driver for macOS

Realtek Card Reader Driver for macOS Unleash the full potential of your SDXC UHS-I cards Introduction An unofficial macOS kernel extension for Realtek

A Lilu plugin that makes System Information recognize your Realtek card reader as a native one
A Lilu plugin that makes System Information recognize your Realtek card reader as a native one

Realtek Card Reader Driver Friend Introduction A Lilu plugin that makes System Information recognize your Realtek card reader as a native one. Support

Lightweight, portable and easy to integrate C directory and file reader

TinyDir Lightweight, portable and easy to integrate C directory and file reader. TinyDir wraps dirent for POSIX and FindFirstFile for Windows. Windows

Lightweight, portable and easy to integrate C directory and file reader

TinyDir Lightweight, portable and easy to integrate C directory and file reader. TinyDir wraps dirent for POSIX and FindFirstFile for Windows. Windows

Schedule Reader but it's written by C++

Schedule Reader 2.0 Automatically convert your excel schedule from your teacher to an iCalendar file that you can import into services like Google Cal

A Simple Nastran to JSON mesh reader which makes it easy to exchange data
A Simple Nastran to JSON mesh reader which makes it easy to exchange data

Nastran to Json Converter A simple code that helps convert Nastran meshes to a JSON file format that is more suitable for the current day and age. Cur

 ESP32 Based ePub Reader
ESP32 Based ePub Reader

ESP32 Based ePub Reader

Simple json reader

JsonReader JsonReader is a library designed to make reading from any json file easy. It is based on jsmn. JsonReader is perfect for working with singl

LoL-Reader is a tool to read game-state.
LoL-Reader is a tool to read game-state.

LoL-Reader LoL-Reader is a tool to read real-time League of Legends statistics from memory. Installation - Compile g++ *.cpp -L. -lPsapi -w -o progra

Comments
  • Invalid ktx_id/ktx magic

    Invalid ktx_id/ktx magic

    Hello!

    There seems to be a problem with loading of KTX files, or specifically with KTX magic at line 900. According to KTX specification (http://github.khronos.org/KTX-Specification/), it should be {0x20, 0x32, 0x30, ...}, but in the file, it is {0x20, 0x31, 0x31...} which is triggering the assertion. Modifying it as suggested above fixed the problem for me.

    Thank you.

    opened by iddev5 3
  • Bug:

    Bug: "ktx: unsupported format" for correct R16UI KTX, easy fix.

    I try decode "terrain_heightmap_r16.ktx" from https://github.com/SaschaWillems/Vulkan and get error "ktx: unsupported format". PWRTexTool open this file without error.

    Way to fix: add to static const ddsktx__ktx_format_info k__translate_ktx_fmt[] this:

       { DDSKTX__KTX_R16I,DDSKTX__KTX_ZERO, DDSKTX__KTX_RED, DDSKTX__KTX_UNSIGNED_SHORT, }, // R16I
       { DDSKTX__KTX_R16UI, DDSKTX__KTX_ZERO, DDSKTX__KTX_RED, DDSKTX__KTX_UNSIGNED_SHORT,  }, // R16UI
    
    opened by Mikez2015 1
  • None POT compressed textures assert failure

    None POT compressed textures assert failure

    Currently the library can't open compressed textures with non-POT sizes.

    test case use crunch without resize flags to compress non-POT texture and try to open it.

    bug 
    opened by septag 1
  • macro errors

    macro errors

    I try to compile it in a simple main.c file:

    #define DDSKTX_IMPLEMENT
    #include "dds-ktx.h"
    
    int main(void)
    {
    	return 0;
    }
    

    But get the following macro errors:

    dds-ktx.h: In function ‘ddsktx__parse_ktx’: dds-ktx.h:911:51: error: macro "ddsktx_memcmp" requires 3 arguments, but only 2 given 911 | if (ddsktx_memcmp(header.id, sizeof(header.id)) == 0) { | ^ In file included from main.c:2: dds-ktx.h:464: note: macro "ddsktx_memcmp" defined here 464 | # define ddsktx_memcmp(_ptr1, _ptr2, _num) memcmp((_ptr1), (_ptr2), (_num)) | In file included from main.c:2: dds-ktx.h:911:9: error: ‘ddsktx_memcmp’ undeclared (first use in this function) 911 | if (ddsktx_memcmp(header.id, sizeof(header.id)) == 0) { | ^~~~~~~~~~~~~ dds-ktx.h:911:9: note: each undeclared identifier is reported only once for each function it appears in dds-ktx.h: In function ‘ddsktx_get_sub’: dds-ktx.h:1197:9: warning: implicit declaration of function ‘sx_assert’; did you mean ‘assert’? [-Wimplicit-function-declaration] 1197 | sx_assert(0 && "invalid file format"); | ^~~~~~~~~ | assert

    opened by ArneDJ 1
Releases(v1.1)
Owner
Sepehr Taghdisian
https://twitter.com/septagh http://glitterbombg.com/blog
Sepehr Taghdisian
Single-header single-function C/C++ immediate-mode camera for your graphics demos

Single-header single-function C/C++ immediate-mode camera for your graphics demos

Nicolas Guillemot 106 Oct 20, 2022
ZBar Bar Code Reader is an open source software suite for reading bar codes from various sources

ZBar Bar Code Reader is an open source software suite for reading bar codes from various sources

null 2.4k Dec 26, 2022
Single header C library for rendering truetype text to the screen

kc_truetypeassembler.h Single header C library for assembling textured quads for text rendering using a graphics API. It generates a vertices and text

Kevin Chin 24 Nov 12, 2022
This is a single-header, multithreaded C++ library for simulating the effect of hydraulic erosion on height maps.

TinyErode This is a single-header, multithreaded C++ library for simulating the effect of hydraulic erosion on height maps. The algorithm is based on

Taylor 43 Aug 4, 2022
A modern, feature-rich single header C++ interface system for GLFW

A modern, feature-rich single header C++ interface system for GLFW

Vortex 3 Dec 27, 2021
SVG animation from multiple SVGs or single GIF using tracer

svgasm svgasm is a proof-of-concept SVG assembler to generate a self-contained animated SVG file from multiple still SVG files with CSS keyframes anim

Tom Kwok 199 Dec 28, 2022
A simple single point light shadow mapping with OpenGL 3.3 and C++

omni-directional-light-example Using OpenGL 3.3 with C++ Basically a single light map, no lighting model was used Usage Build the executable outside A

Mohammad Issawi 4 Feb 10, 2022
simple fdtd using vulkan, omp or single thread

fdtd simple fdtd using vulkan, omp or single thread example how to build first clone the repo with: git clone https://github.com/nikisalli/fdtd.git up

Nik 5 Nov 12, 2022
A minimal Direct3D 12 example that draws an animated triangle, written entirely in C-style C++, and all taking place inside a single function.

A minimal Direct3D 12 example that draws an animated triangle, written entirely in C-style C++, and all taking place inside a single function.

Taoufik Rida Bouftass 7 May 3, 2022
CXXGraph is a small library, header only, that manages the Graph and it's algorithms in C++.

CXXGraph is a small library, header only, that manages the Graph and it's algorithms in C++.

ZigRazor 184 Dec 28, 2022