Skip to content

Decode auxiliary alpha stored as a grid - #32

Merged
laurenspriem merged 7 commits into
ente:mainfrom
laurens-pilot:feat/grid-alpha
Nov 3, 2026
Merged

laurenspriem merged 7 commits into
ente:mainfrom
laurens-pilot:feat/grid-alpha

Conversation

@laurens-pilot

Copy link
Copy Markdown

Transparent HEICs from Apple's ImageIO store their alpha as a grid item, 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:

  • An alpha grid whose size differs from the primary is skipped before any of its tiles are copied.
  • Tiles that reference the same item share one copy of its payload. HeicGridTileItemData::payload becomes an Arc<Vec<u8>>.
  • Tile payloads and ICC profiles that are actually copied are capped at four times the file size, with no 128 MiB floor. The existing per-reference budget still applies.

Peak memory on crafted files, before → after:

  • 132 KB file from the review, 65,025 references to one 2 KB tile: 142 MB → 3.5 MB (7 MB as a primary grid)
  • 536 KB file, 256 distinct tiles over the same bytes: 139 MB → 6.5 MB, on either grid path
  • 908 KB file, one ICC profile across 120 tiles: 114 MB → 9.7 MB

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

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.
@laurens-pilot

Copy link
Copy Markdown
Author

@codex review

@laurenspriem
laurenspriem disabled auto-merge November 3, 2026 16:34
@laurenspriem
laurenspriem merged commit 288c421 into ente:main Nov 3, 2026
3 checks passed
@laurens-pilot
laurens-pilot deleted the feat/grid-alpha branch November 3, 2026 16:37
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