Repository navigation
Decode auxiliary alpha stored as a grid - #32
Merged
Merged
Conversation
ImageIO stores the alpha of every transparent image with a side of 1024 px or more as a grid item tiled like the primary. The decoder only accepted coded alpha items, so all such images decoded fully opaque without an error. Extract a grid alpha item through the shared grid tile extraction, preflight it with the caller's guardrails, compose it with the existing grid decoder and use its luma plane as the alpha. Alpha decoded from ImageIO files between 1024x700 and 4096x4096 now matches ImageIO's own decode exactly. Only the first grid candidate that is an alpha item gets extracted, so a file listing thousands of crafted grid alpha candidates costs one tile budget instead of one per candidate: 3,000 candidates in a 2 MB file took 6.3 s and now take 30 ms. Coded alpha candidates keep their existing fallback.
A grid can reference the same tile item many times. Each reference got its own copy of the tile's data, so a 132 KB file whose 65,025 grid cells all point at one 2 KB tile made the decoder hold about 127 MiB of identical copies. Tiles that reference the same item now share one copy of its payload, as they already share its hvcC, colour and transform properties. The extracted buffer is moved into the Arc rather than copied, so peak memory for a single large tile does not grow. The tile data budget still counts every reference, so the set of accepted files does not change. HeicGridTileItemData::payload becomes an Arc<Vec<u8>>.
A grid alpha item of the wrong size was only rejected after all of its tiles had been copied. The grid descriptor is now read first, and an alpha grid whose output size differs from the primary image is skipped before any tile is extracted. The primary's size has already passed the caller's max_pixels limit, so this also holds the alpha grid to that limit before anything is copied.
A grid alpha item that fails to decode is dropped, so the image is returned without alpha, while running out of memory is reported as an error like other allocation failures. Nothing tested either half of this on the grid path.
The grid tile data budget is max(4 x file size, 128 MiB). The 128 MiB floor let a small file make the decoder copy far more data than it holds: distinct tile items whose locations overlap the same bytes each got their own copy, and one large ICC profile was copied once per tile colour combination. A 536 KB file reached about 139 MB of tile copies before preflight could reject the grid, on both the primary and the alpha path. Tile payloads and colour profiles that are actually copied are now also held to four times the file size, with no floor. Repeated references to one tile share a copy, so they do not count against this cap, and the existing per-reference budget still applies. The 536 KB file now peaks at 6.5 MB, and a 908 KB file repeating one ICC profile across 120 tiles drops from 114 MB to 9.7 MB.
laurenspriem
enabled auto-merge
November 3, 2026 16:23
Author
|
@codex review |
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.
Transparent HEICs from Apple's ImageIO store their alpha as a
griditem, tiled like the primary, whenever a side is 1024 px or more. The decoder only accepted single coded alpha items, so these images decoded fully opaque without an error.Grid alpha now goes through the shared grid tile extraction, is preflighted with the caller's guardrails, and is composed by the existing grid decoder; its luma plane becomes the alpha. Only the first grid candidate that is an alpha item is extracted, so a crafted file listing thousands of candidates costs one tile budget.
The security review's note is addressed for both grid paths:
HeicGridTileItemData::payloadbecomes anArc<Vec<u8>>.Peak memory on crafted files, before → after:
On 23 ImageIO transparent files from 512×512 to 4096×4096, RGBA output now matches libheif 1.21.2 pixel for pixel. Across 305 corpus and ImageIO files, only the 20 files with grid alpha change output. A transparent 4096×4096 image takes about 30 MB more peak memory, roughly the size of the alpha plane.
Alpha planes whose size differs from the primary are still ignored (see
FUTURE_WORK.md).Source fork PR: laurens-pilot#19
@laurenspriem