Skip to content

HDR-to-8-bit output is rejected by the CLI and changes decoded API colors #675

Closed

Description

@vera-rubin

The documented HDR-to-TGA command fails before decoding with ASTCENC_ERR_BAD_DECODE_MODE. The API also returns RGB 133 when a valid HDR block encoding exactly 0.5 is decoded to 8-bit storage, where the supported UNORM8 conversion gives 128.

Version and build

ARM-software/astc-encoder 5.7.0, commit 18b20e6641503c41b2aac55664a2400534ba7212. Both cases were reproduced on Linux x86_64 with the SSE2 decompressor and the native full encoder using AVX2, POPCNT and F16C.

Tools: GNU C++ 13.3.0, CMake 3.28.3, GNU Make 4.3 and Python 3.12.3. Current main was read on 2026-10-12 and matches the tested commit.

These shell commands build the SSE2 implementation used by both examples:

work=$(mktemp -d)
git clone https://github.com/ARM-software/astc-encoder.git "$work/astc-encoder"
git -C "$work/astc-encoder" checkout --detach 18b20e6641503c41b2aac55664a2400534ba7212
cmake -S "$work/astc-encoder" -B "$work/sse2" \
  -DCMAKE_BUILD_TYPE=Release -DASTCENC_DECOMPRESSOR=ON \
  -DASTCENC_ISA_SSE2=ON -DASTCENC_SHAREDLIB=OFF
cmake --build "$work/sse2" --target astcdec-sse2 -j2

Case 1: documented HDR-to-TGA command

Expected: HDR decoding to TGA succeeds. The pinned README, lines 158–169 gives astcenc -dh example.astc example.tga and states that -dh and -dH have the same decompression behavior.

The input is one 4×4×1 block in a 32-byte .astc file. Its HDR void-extent payload has all-one extent coordinates and finite FP16 RGBA (0.5, 0.5, 0.5, 1.0). This layout is defined by Khronos Data Format Specification 1.4.0, section 23.22 and the project's file-format document.

Save this as $work/repro-cli.py:

#!/usr/bin/env python3
"""Run the documented HDR-to-TGA operation on a valid 4x4 HDR void block.
All generated inputs/outputs stay under JOB_WORK (or a directory given as arg 2).
"""
import os
from pathlib import Path
import struct
import subprocess
import sys
import tempfile

if len(sys.argv) not in (2, 3):
    raise SystemExit("usage: python3 repro.py /path/to/astcenc-or-astcdec [work-directory]")
binary = str(Path(sys.argv[1]).resolve())
work = Path(sys.argv[2] if len(sys.argv) == 3 else os.environ["JOB_WORK"])
work.mkdir(parents=True, exist_ok=True)
# .astc header: magic, block footprint 4x4x1, image dimensions 4x4x1.
header = bytes.fromhex("13aba15c040401040000040000010000")
# KDFS 1.4.0 section 23.22: HDR flag=1, all-one void extents,
# four FP16 components 0.5, 0.5, 0.5, 1.0. Valid and finite.
hdr_block = bytes.fromhex("fcffffffffffffff") + struct.pack("<4H", 0x3800, 0x3800, 0x3800, 0x3c00)
ldr_block = bytes.fromhex("fcfdffffffffffff") + struct.pack("<4H", 0x8000, 0x8000, 0x8000, 0xffff)
passed = True
with tempfile.TemporaryDirectory(prefix="astc-hdr-u8-cli-", dir=work) as directory:
    root = Path(directory)
    hdr = root / "hdr.astc"
    ldr = root / "ldr.astc"
    hdr.write_bytes(header + hdr_block)
    ldr.write_bytes(header + ldr_block)
    for label, mode, source, extension in [
        ("LDR-to-TGA control", "-dl", ldr, "tga"),
        ("HDR-to-EXR control", "-dh", hdr, "exr"),
        ("documented HDR-to-TGA", "-dh", hdr, "tga"),
        ("HDR RGBA-to-TGA", "-dH", hdr, "tga"),
    ]:
        target = root / (label.replace(" ", "-") + "." + extension)
        result = subprocess.run([binary, mode, str(source), str(target), "-silent"],
                                stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
        success = result.returncode == 0 and target.is_file() and target.stat().st_size > 0
        print(f"{label}: exit={result.returncode}, output_exists={target.is_file()}, expected=success")
        if result.stdout.strip():
            print(result.stdout.strip())
        passed &= success
print("PASS" if passed else "FAIL: supported HDR-to-TGA operation was rejected")
raise SystemExit(0 if passed else 1)

Run it in the same shell as the build commands:

python3 "$work/repro-cli.py" "$work/sse2/Source/astcdec-sse2" "$work"

Observed on both builds:

LDR-to-TGA control: exit=0, output_exists=True, expected=success
HDR-to-EXR control: exit=0, output_exists=True, expected=success
documented HDR-to-TGA: exit=1, output_exists=False, expected=success
ERROR: Codec context alloc failed: ASTCENC_ERR_BAD_DECODE_MODE
HDR RGBA-to-TGA: exit=1, output_exists=False, expected=success
ERROR: Codec context alloc failed: ASTCENC_ERR_BAD_DECODE_MODE
FAIL: supported HDR-to-TGA operation was rejected

The script exits 1. Both HDR commands create no TGA file. The same HDR input writes EXR successfully, and the LDR control writes TGA successfully.

Case 2: exact 0.5 HDR color stored as RGB 133

Expected: both blocks below decode to FP16 RGBA (0x3800, 0x3800, 0x3800, 0x3c00) and U8 RGBA (128, 128, 128, 255).

The regular block has mode 0x013, a 4×2 weight grid, one partition, HDR luminance endpoint mode 2, endpoint integers v0 = v1 = 112 and zero weights. KDFS 1.4.0, sections 23.14.2.1 and 23.18.2 give RGB C = 0x7000, E = 14, M = 0 and Cf = 0x3800, exactly 0.5. Alpha is exactly 1.0. The HDR void-extent block encodes the same color directly.

HDR decoding and UNORM8 output storage are described as separate operations in issue 611. The U8 store at astcenc_image.cpp:402–408 clamps to [0,1], scales by 255 and rounds. Thus 0.5 maps to 128.

Save this as $work/repro-api.cpp:

// KDFS 1.4.0, sections 23.10, 23.14.2.1, 23.18.2, 23.22.
// A valid 4x4 block: mode 0x013 selects a 4x2 grid, range 0..7,
// one plane. CEM=2, endpoint integers v0=v1=112, all weights zero.
// HDR endpoints: y0=y1=112<<4=1792. Interpolator C=1792<<4=0x7000.
// E=14, M=0, Mt=0; Cf=14<<10=0x3800 = exactly 0.5.
// Alpha Cf=0x3c00 = 1.0. A UNORM8 image represents 0.5 as 128.
#include "astcenc.h"
#include <array>
#include <cstdint>
#include <cstdio>

static bool check(astcenc_error error) {
    if (error == ASTCENC_SUCCESS) return true;
    std::fprintf(stderr,"API error: %s\n",astcenc_get_error_string(error));
    return false;
}
int main() {
    alignas(16) const uint8_t regular[16] = {
        0x13,0x40,0xe0,0xe0,0,0,0,0,0,0,0,0,0,0,0,0
    };
    // Same exact color in a valid HDR void block (all-one extent coordinates).
    alignas(16) const uint8_t constant[16] = {
        0xfc,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
        0x00,0x38,0x00,0x38,0x00,0x38,0x00,0x3c
    };
    astcenc_config config {};
    if (!check(astcenc_config_init(ASTCENC_PRF_HDR,4,4,1,ASTCENC_PRE_FASTEST,
                                  ASTCENC_FLG_DECOMPRESS_ONLY,&config))) return 2;
    astcenc_context* ctx=nullptr;
    if (!check(astcenc_context_alloc(&config,1,&ctx,nullptr))) return 2;
    astcenc_swizzle swz {ASTCENC_SWZ_R,ASTCENC_SWZ_G,ASTCENC_SWZ_B,ASTCENC_SWZ_A};
    bool pass=true;
    for (int i=0;i<2;i++) {
        const uint8_t* block=i ? constant : regular;
        std::array<uint16_t,64> f16 {};
        std::array<uint8_t,64> u8 {};
        void* fplanes[1]={f16.data()};
        void* uplanes[1]={u8.data()};
        astcenc_image fimage {4,4,1,ASTCENC_TYPE_F16,fplanes};
        astcenc_image uimage {4,4,1,ASTCENC_TYPE_U8,uplanes};
        if (!check(astcenc_decompress_image(ctx,block,16,&fimage,&swz,0)) ||
            !check(astcenc_decompress_image(ctx,block,16,&uimage,&swz,0))) return 2;
        for (size_t j=0;j<64;j++) {
            uint16_t expected_f16=j%4==3 ? 0x3c00 : 0x3800;
            uint8_t expected_u8=j%4==3 ? 255 : 128;
            if (f16[j]!=expected_f16 || u8[j]!=expected_u8) pass=false;
        }
        std::printf("%s: FP16=(%04x,%04x,%04x,%04x), U8=(%u,%u,%u,%u); expected U8=(128,128,128,255)\n",
                    i ? "void" : "regular",f16[0],f16[1],f16[2],f16[3],u8[0],u8[1],u8[2],u8[3]);
    }
    astcenc_context_free(ctx);
    std::puts(pass ? "PASS" : "FAIL: equivalent HDR colors produce different 8-bit results");
    return pass ? 0 : 1;
}

Build and run against the same SSE2 library:

g++ -std=c++14 -O2 -I "$work/astc-encoder/Source" \
  "$work/repro-api.cpp" "$work/sse2/Source/libastcdec-sse2-static.a" \
  -pthread -o "$work/repro-api-sse2"
"$work/repro-api-sse2"

Observed on both builds:

regular: FP16=(3800,3800,3800,3c00), U8=(133,133,133,255); expected U8=(128,128,128,255)
void: FP16=(3800,3800,3800,3c00), U8=(128,128,128,255); expected U8=(128,128,128,255)
FAIL: equivalent HDR colors produce different 8-bit results

The program checks every component of all 16 texels and exits 1. The API calls succeed. It requests only ASTCENC_FLG_DECOMPRESS_ONLY, with one worker and identity swizzle.

To repeat both examples with the native full encoder on a compatible x86_64 host:

cmake -S "$work/astc-encoder" -B "$work/native" \
  -DCMAKE_BUILD_TYPE=Release -DASTCENC_DECOMPRESSOR=OFF \
  -DASTCENC_ISA_NATIVE=ON -DASTCENC_SHAREDLIB=OFF
cmake --build "$work/native" --target astcenc-native -j2
python3 "$work/repro-cli.py" "$work/native/Source/astcenc-native" "$work"
g++ -std=c++14 -O2 -I "$work/astc-encoder/Source" \
  "$work/repro-api.cpp" "$work/native/Source/libastcenc-native-static.a" \
  -pthread -o "$work/repro-api-native"
"$work/repro-api-native"

Shared source diagnosis

Both sites treat 8-bit output storage as the LDR-only decode_unorm8 mode without checking the HDR profile.

The CLI at astcenccli_toplevel.cpp:2020–2036 adds ASTCENC_FLG_USE_DECODE_UNORM8 whenever output bitness is 8. Flag validation at astcenc_entry.cpp:312–317 rejects that flag with either HDR profile, explaining case 1.

The API at astcenc_entry.cpp:1338–1339 infers blk.decode_unorm8 from ASTCENC_TYPE_U8. The component mask then enables bit replication before HDR conversion. In case 2, 0x7000 becomes 0x7070, which converts to FP16 0x382a (0.5205078125) and then U8 133. The HDR void-extent path bypasses that interpolation step.

Both assignments originate in commit 62931077e35050bde5850aa0223a228d9e3c4cde. A suggested correction is to guard LDR decode-mode behavior by profile at both sites while retaining HDR-to-U8 storage conversion. No fix was applied or tested in the independent verification.

Scope and controls

Testing used valid, finite 4×4 inputs on unchanged source. Both HDR profiles were checked. The independent CLI check also reproduced the rejection for PNG and BMP. Fresh-context API checks reproduced RGB 133 with U8 output, while F16 and F32 output preserved 0.5. The void-extent and LDR API controls passed.

The reproducers do not request ASTCENC_FLG_USE_DECODE_UNORM8. KDFS 1.4.0, section 23.5 does not define valid HDR results for that decode mode. These cases concern the project's documented HDR-to-8-bit storage conversion.

Other architectures, operating systems, block footprints, endpoint modes, older releases and compression round trips were not tested for these findings.

The reproducer programs and hand-assembled inputs above are provided under CC0-1.0.

Found by ChipLab's AI verification agents and independently reproduced.

ChipLab ref: CLAB-2026-788

Activity

  1. deleted a comment from Dreamer1 on Oct 12, 2026
  2. solidpixel commented on Oct 12, 2026

    @solidpixel
    Contributor

    I mostly view this as a readme bug - .tga is an LDR image container, and cannot support HDR data, so should not be used for the HDR example command line. If an application does, they can't really have any expectation of sensible output.

    The rest is working as designed.

  3. solidpixel commented on Oct 15, 2026

    @solidpixel
    Contributor

    Documentation fix merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions