Skip to content

fix(Texture2D): RGBAHalf and RGB9e5Float - #400

Open
isHarryh wants to merge 1 commit into
K0lb3:masterfrom
isHarryh:fix/tex2d-conv
Open

isHarryh wants to merge 1 commit into
K0lb3:masterfrom
isHarryh:fix/tex2d-conv

Conversation

@isHarryh

Copy link
Copy Markdown
Contributor

Summary

This PR fixes texture parsing of RGBAHalf and RGB9e5Float format.

This PR partially solves #386 .

Verification

import struct

from UnityPy.enums import BuildTarget
from UnityPy.enums import TextureFormat as TF
from UnityPy.export.Texture2DConverter import parse_image_data


def decode(texture_format, data, width, height):
    return parse_image_data(
        data,
        width,
        height,
        texture_format,
        (2021, 3, 56, 0),
        BuildTarget.StandaloneWindows,
        flip=False,
    )


def test_rgba_half_preserves_alpha_and_channel_alignment():
    values = [0.0, 0.25, 0.5, 1.0, 1.0, 0.5, 0.25, 0.0]
    img = decode(TF.RGBAHalf, struct.pack("<8e", *values), 2, 1)

    assert img.mode == "RGBA"
    assert img.getpixel((0, 0)) == (0, 64, 128, 255)
    assert img.getpixel((1, 0)) == (255, 128, 64, 0)


def test_rgb9e5float_decodes_and_clamps():
    def encode(exponent, r, g, b):
        return struct.pack("<I", (exponent << 27) | (b << 18) | (g << 9) | r)

    data = b"".join(
        [
            encode(16, 64, 128, 256),  # 0.25, 0.5, 1.0
            encode(31, 511, 511, 511),  # HDR values are clamped to 255
            encode(0, 0, 0, 0),
        ]
    )
    img = decode(TF.RGB9e5Float, data, 3, 1)

    assert img.mode == "RGB"
    assert img.getpixel((0, 0)) == (64, 128, 255)
    assert img.getpixel((1, 0)) == (255, 255, 255)
    assert img.getpixel((2, 0)) == (0, 0, 0)

@K0lb3

K0lb3 commented Nov 19, 2026

Copy link
Copy Markdown
Owner

It's actually a more complex issue with also other still unfixed stuff.

i should be I for RGBE9E5.
RGBE9E5 and likely other formats are also linear, not SRGB, not their exported version will likely look a bit different.

Overall I think.....that PIL simply doesn't cut it for formats like this,
and opencv or other better image libraries have to be used.

As opencv now ships wheels instead of requiring some local library on Linux and Mac,
it's probably time to switch to it instead of trying to work around the limitations of PIL.

@isHarryh

Copy link
Copy Markdown
Contributor Author

Thanks for the review.

  1. I agree I is the correct type for the packed RGB9e5 value while i also works correctly since the masks make it behavior-neutral, so I'll switch it as a small cleanup.

  2. I'd prefer to handle color space separately using Texture2D.m_ColorSpace, because a blanket sRGB conversion would also affect data textures, and the current image API returns raw values.

  3. On OpenCV: I agree Pillow has limitations for float/HDR formats, but switching the whole converter is a much larger architectural change. OpenCV would not directly decode Unity's packed RGB9e5 layout either.

I'd keep this PR focused first.

return Image.frombytes("RGBA", (width, height), image_data, "raw", "BGRA")


def _float_to_uint8(value: float) -> int:

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imo it might be better to do this as an array operation,
as otherwise there is a sizeable performance hit.
Might as well split it into clamping and converting to int at that point.

@isHarryh isHarryh Nov 20, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer not to add NumPy or OpenCV as a hard dependency in this bug-fix PR, since NumPy is still optional in the project and the OpenCV switch is a much larger architectural change.

So I can either:

  1. Optimize this without new dependencies, using Pillow's I;16 mode plus a 64k LUT for half floats and a 32×512 LUT for RGB9e5. (~200% speedup)

  2. Keep this PR focused on correctness and move the performance work to a follow-up PR.

This branch has not been deployed

No deployments
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