Add runtime-aware gain map routing predicate - #462
Open
gregbenz wants to merge 2 commits into
Open
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
gregbenz
marked this pull request as ready for review
August 27, 2026 01:20
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This adds a small public API for image loaders that need to decide whether to route an in-memory image to libultrahdr:
If it returns
1, the caller can try libultrahdr. If it returns0, the caller can continue through its normal image path. Decode errors are still handled normally after a positive result.The main motivation is downstream image libraries that want to enable gain-map support by default. Loaders that already have the complete input in memory can use this predicate directly, without asking applications or users to opt in or implement their own format probes. The same API works across JPEG, AVIF, and HEIF integrations.
API behavior
is_uhdr_image()remains a structural check: does the input contain a primary image, a gain-map image, and valid gain-map metadata?The new predicate answers the narrower routing question: does the input have a gain-map form that this build and runtime are prepared to handle? For AVIF/HEIF, this also checks the required decoder families. A structurally valid image can therefore return
1fromis_uhdr_image()and0fromuhdr_is_supported_gainmap_image().A positive result is intentionally a routing hint, not a guarantee that every codec profile or damaged compressed stream will decode successfully. Avoiding a trial pixel decode keeps this check inexpensive.
Implementation
Both public predicates use the same stateless, borrowed-buffer structural inspection:
0. C++ exceptions are contained at the C ABI when enabled, and the implementation also builds with exceptions disabled.This does make
is_uhdr_image()stricter for malformed JPEGs with missing, ambiguous, or incorrect MPF associations. Decoder setup and decode APIs are otherwise unchanged.The existing stateful
uhdr_dec_probe()path is also unchanged.uhdr_dec_set_image()owns a copy of its input, anduhdr_dec_probe()populates state needed by a later decode; changing that lifecycle is outside the scope of this stateless dispatch API.This patch does not add a typed result API, streaming/prefix input, a general item-graph abstraction, or pixel-level trial decoding.
Validation
0.1structurally and0from the routing predicate. With HEVC available, the same fixture probes and decodes successfully.MPF\0signature.Performance
These are release-build averages over 1,000 calls on arm64 macOS 26.6.2 with Apple Clang 21. Inputs were already resident in memory, so the measurements exclude file I/O. Encoded size is included because probe cost depends more on byte and segment layout than pixel dimensions; these are representative fixture measurements, not size-independent guarantees.
For comparison, the previous
is_uhdr_image()path measured 19.0 us for the Apple JPEG, 156.5 us for the ISO JPEG, and 33.2 us for the gain-map AVIF. The new path does not duplicate the complete encoded stream or embedded JPEG payloads, although the parsers may still allocate container and metadata structures.The proposed name, Boolean contract, and 2.1.0 version are open to maintainer preference. The important part for downstream callers is having one compact, conservative routing decision that is safe to enable by default.