Skip to content

Tweak decoders, add encoder and CLI - #2

Open
jmhobbs wants to merge 6 commits into
mauserzjeh:masterfrom
jmhobbs:add-encoding
Open

Tweak decoders, add encoder and CLI#2
jmhobbs wants to merge 6 commits into
mauserzjeh:masterfrom
jmhobbs:add-encoding

Conversation

@jmhobbs

@jmhobbs jmhobbs commented Jul 24, 2026

Copy link
Copy Markdown

This PR does a couple things;

  1. It tweaks decode to match spec a bit closer
  2. It adds an encoder for each format
  3. It adds a CLI for encoding/decoding to each (mostly just for testing)

Some decisions were made around quantization and alpha on DXT1/DXT3 which could be configurable, but are not. For example DXT1 "alpha" is on/off, so we split the RGBA alpha down the middle and turn it on/off at 128.

As an example, here are the outputs of a round-trip through the CLI (encode to DXT*, decode back to PNG) for this test file:

Original alpha-test-512x512
DXT1 alpha-test dxt1
DXT3 alpha-test dxt3
DXT5 alpha-test dxt5

jmhobbs added 6 commits July 22, 2026 16:26
DecodeDXT1 never implemented the 3-color + transparent-black mode: color3 always used the opaque interpolation regardless of color0 vs color1, instead of decoding to black when color0 <= color1.

DecodeDXT3 and DecodeDXT5 incorrectly branched color2 on the actual color0/color1 relationship. Per spec their color block must always use the opaque encoding, treating color0 as though it were greater than color1 regardless of the real values. Replaced that branch with a new c2_opaque() used only by DXT3/DXT5.
DecodeDXT5 built its 48-bit alpha index field starting at the block's first byte, which is actually alpha0.

According to spec, alpha0/alpha1 occupy bytes 0-1 and the 48-bit index field is the following 6 bytes.
The existing code read its first index bits out of the alpha reference bytes and never read the block's last 2 bytes at all.

testdata/dxt5.decoded was regenerated from the corrected decoder
The dxt package deals in non-premultiplied alpha bytes, which NRGBA is
and RGBA is not.
@mauserzjeh

Copy link
Copy Markdown
Owner

Thanks for your contribution! I will do a more throughout review during the weekend and merge it. At first glance your PR looks great and enhances library. Special thanks for creating a CLI tool as well

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants