Repository navigation
Match libheif on clean-aperture crops with an odd origin - #37
Merged
laurenspriem merged 3 commits intoNov 24, 2026
Merged
Conversation
libheif converts a 4:2:0 image to 4:4:4 with bilinear chroma before a clap crop whose left or top edge is odd (odd left for 4:2:2). For limited-range coded colour its conversion targets a default full-range profile, so it also passes the image through float RGB back to full-range 4:4:4 before cropping, and applies a colr nclx override only afterwards. The regular RGBA, RGB8 and image-hook paths now do the same for primary items, whole grids and individual grid tiles, instead of cropping RGBA converted with nearest-neighbour chroma. The incremental corpus now goes through the exact heif-dec verify: generated grid tiles are hidden so heif-dec writes only the primary image, the corpus is part of the default verify corpus, CI generates it before the full pass, and new tile-crop cases cover clean apertures on grid tiles.
The odd-origin crop path upsampled the whole image to 16-bit 4:4:4 and then cropped it, so a 7999x8000 grid briefly held about 768 MB of YUV planes on top of the canvas and output. The bilinear upsampling, limited range round trip and colr override now run on bands of about 256K pixels taken straight from the 4:2:0 planes, and each band is converted to RGB as soon as it is built. Peak heap for that grid on the RGB8 path drops from 768 MB to 387 MB, and the output is byte-identical.
Author
|
@codex review |
This was referenced Nov 24, 2026
laurenspriem
added a commit
to ente/ente
that referenced
this pull request
Nov 26, 2026
Update `ente-image`'s HEIC decoder pin from `5fbe1d2` to [`5b0a6af`](ente/heic-decoder@5fbe1d2...5b0a6af). It brings: - Faster bounded decoding of grid HEICs (ente/heic-decoder#41). Measured there on a Pixel 10, `decode_bounded` took 0.62–0.77× as long as the regular decoder on 12–24 MP phone HEICs, against 1.14–1.29× before. This is the path ML indexing, face thumbnails, OCR and the internal Android HEIC viewer use. - `decode_bounded` now decodes HEICs with an odd-origin crop or an odd-sized rotation or mirror instead of refusing them (ente/heic-decoder#39). - The regular decoder now matches libheif on those crops, rotations and mirrors in HEIC and AVIF, converting chroma to 4:4:4 before the transform (ente/heic-decoder#37, #38, #40). A grid whose tiles need different conversions now fails, as it does in libheif. - AVIF items without a `colr` nclx property now use the AV1 stream's range and matrix (ente/heic-decoder#40). On 309 HEIC, HEIF and AVIF files (ente's test fixtures, phone HEICs, libheif's examples, fuzz and stress files, and heic-decoder's generated corpora), both decoders give the same pixels or the same refusal under both pins on 294, including every real phone photo. The other 15 are generated odd-crop and odd-size rotation and mirror files: the regular decoder now gives different pixels, and `decode_bounded` decodes them instead of refusing, with exactly the regular decoder's output. Source fork PR: laurens-pilot#308 @laurenspriem Co-authored-by: laurenspriem <laurens-pilot-git@lrns.me>
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.
When a 4:2:0
clapstarts on an odd column or row (an odd column for 4:2:2), libheif converts the image to 4:4:4 with bilinear chroma before cropping. heic-decoder cropped RGBA converted with nearest-neighbour chroma instead. It differed by up to 7 on photographic fixtures and about 191 on synthetic high-frequency chroma. The regular RGBA, RGB8 and image-hook paths now follow libheif for primary items, whole grids and individual grid tiles.Two more libheif details were needed for an exact match:
colrnclx is applied only afterwards.The 4:4:4 image is never held at full size. It is built in bands of about 256K pixels straight from the 4:2:0 planes, and each band goes to RGB right away. A grid that needs the conversion is assembled into one 4:2:0 canvas first. For a 7999×8000 grid with an odd-origin crop, peak heap on the RGB8 path is 387 MB, against 768 MB when the whole image is converted first and 239 MB for the same grid without the crop.
A grid whose tile crops convert some tiles but not others now fails with
DecodedLayoutMismatch. Main decoded it; libheif rejects it.Image edges use the clamped kernel from libheif's later border fix (strukturag/libheif 215cbdad), which the pinned version predates.
The odd-crop fixtures now go through the exact
heif-decverify:heif-decwrote a tile first and verify compared the primary decode against it.tile-crop/cases cover clean apertures on grid tiles. They sit in a subdirectory because the incremental and bounded decoders refuse tile crops.Locally, all 16 incremental-corpus files pass exactly; upstream main fails the nine odd-crop ones. Local incremental corpora made before this change need regenerating.
decode_boundedstill refuses these crops; matching libheif there is a separate follow-up.Source fork PR: laurens-pilot#25
@laurenspriem