Skip to content

Match libheif on clean-aperture crops with an odd origin - #37

Merged
laurenspriem merged 3 commits into
ente:mainfrom
laurens-pilot:fix/odd-clap-bilinear-chroma
Nov 24, 2026
Merged

laurenspriem merged 3 commits into
ente:mainfrom
laurens-pilot:fix/odd-clap-bilinear-chroma

Conversation

@laurens-pilot

Copy link
Copy Markdown

When a 4:2:0 clap starts 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:

  • Its conversion targets a default full-range profile. Limited-range coded colour therefore also goes through float RGB and back to full-range 4:4:4 before the crop.
  • That conversion uses the coded stream's colour. A colr nclx 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-dec verify:

  • Generated grid tiles are hidden. Before, heif-dec wrote a tile first and verify compared the primary decode against it.
  • The incremental corpus joins the default verify corpus, and CI generates it before the full pass.
  • Three new 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_bounded still refuses these crops; matching libheif there is a separate follow-up.

Source fork PR: laurens-pilot#25

@laurenspriem

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

Copy link
Copy Markdown
Author

@codex review

@laurenspriem
laurenspriem merged commit 43e7800 into ente:main Nov 24, 2026
3 checks passed
@laurens-pilot
laurens-pilot deleted the fix/odd-clap-bilinear-chroma branch November 24, 2026 11:43
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>
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