Skip to content

Match libheif when rotating or mirroring odd 4:2:0 and 4:2:2 images - #38

Merged
laurenspriem merged 1 commit into
ente:mainfrom
laurens-pilot:fix/odd-transform-bilinear-chroma
Nov 24, 2026
Merged

laurenspriem merged 1 commit into
ente:mainfrom
laurens-pilot:fix/odd-transform-bilinear-chroma

Conversation

@laurens-pilot

Copy link
Copy Markdown

libheif also converts an image to 4:4:4 with bilinear chroma before some rotations and mirrors:

  • 4:2:0: before an irot that reverses an odd side, and before any imir when either side is odd.
  • 4:2:2: before a 90° or 270° irot, a 180° irot of an odd height, or a left-right imir of an odd width. An odd-width 4:2:2 image's half-width chroma planes are rotated by 180° without conversion.

heic-decoder rotated and mirrored RGBA converted with nearest-neighbour chroma instead, and differed by up to 92 when an odd-width crop was followed by such a rotation or mirror. The regular RGBA, RGB8 and image-hook paths now walk the transforms the way libheif does, tracking the image size. They apply the transforms before the first conversion to the YUV planes. The conversion then builds the 4:4:4 image in bands, with the same limited-range round trip and late colr override as the odd-origin crop in #25, and the rest goes to the RGBA path. Grids that need this go through the assembled canvas. For a 7999×8000 grid with irot 1, peak heap on the RGB8 path is 387 MB, against 768 MB when the whole image is converted first.

The generated corpus gains an orientation/ subdirectory of 13 cases, which default verify compares exactly against heif-dec:

  • The 4:2:0 cases rotate or mirror an odd grid canvas, then trim one pixel from every edge with a clap, because the pinned libheif's 4:2:0 bilinear border is defective.
  • The 4:2:2 cases keep their edges, since that kernel has no defect.

wrap.rb gains TRANSFORM_ORDER and now takes the chroma format from the SPS.

This is stacked on #25. Locally, all 29 files of the regenerated incremental corpus pass the exact verify. decode_bounded still refuses these transforms.

Source fork PR: laurens-pilot#26

@laurenspriem

libheif converts a 4:2:0 image to 4:4:4 with bilinear chroma before an
irot that reverses an odd side, and before any imir when either side is
odd. It converts a 4:2:2 image before a 90 or 270 degree irot, a 180
degree irot of an odd height, and a left-right imir of an odd width, but
rotates the half-width chroma planes of an odd-width 4:2:2 image by 180
degrees without converting them. The regular RGBA, RGB8 and image-hook
paths now walk the transforms as libheif does, tracking the image size:
they apply the transforms before the first conversion to the YUV planes,
then build the 4:4:4 image in bands with the limited-range round trip
and late colr override the odd clap crop already models, and leave the
rest to the RGBA path. Grids that need this go through the assembled
canvas.

generate.rb adds an orientation subdirectory that default verify
compares exactly against heif-dec. The 4:2:0 cases rotate or mirror an
odd grid canvas and then trim one pixel from every edge with a clap,
because the pinned libheif's 4:2:0 bilinear border is defective. The
4:2:2 cases keep their edges, as that kernel has no defect. wrap.rb
gains TRANSFORM_ORDER and takes the chroma format from the SPS.
@laurens-pilot

Copy link
Copy Markdown
Author

@codex review

1 similar comment
@laurens-pilot

Copy link
Copy Markdown
Author

@codex review

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