Skip to content

Align CPU gain-map sampling with pixel centers - #503

Open
gregbenz wants to merge 1 commit into
google:mainfrom
gregbenz:codex/gainmap-centered-sampling-20261019
Open

gregbenz wants to merge 1 commit into
google:mainfrom
gregbenz:codex/gainmap-centered-sampling-20261019

Conversation

@gregbenz

Copy link
Copy Markdown
Contributor

CPU HDR reconstruction currently samples reduced gain maps from their top-left texel positions and uses the horizontal scale for both axes. When the aspect ratios differ by more than 1%, it switches to resizing the map into a full-resolution byte buffer first. GLES uses pixel-centered bilinear texture sampling with independent horizontal and vertical scales. This can place the gain transition differently depending on the decoding backend and map dimensions.

This change samples the original gain map directly at corresponding pixel centers, with independent X/Y scales and clamped bilinear filtering. It retains a direct read for maps whose width and height both match the base, and avoids the full-resolution temporary map and its extra 8-bit rounding step. It applies to grayscale and RGB/RGBA maps in the shared CPU reconstruction path. Public APIs, encoded metadata, the editor resize implementation and GLES are unchanged.

This addresses a different issue from #502's correction to resize_image(). The centered-sampling regression fails with that patch applied too. This change leaves the helper and its tests intact, but removes the decoder's use of it. Reduced-map CPU output changes intentionally; CPU and GLES still have other numerical differences, so their final HDR output is not expected to match bit-for-bit.

The new synthetic regression covers grayscale, RGB and RGBA maps, independent axis scaling, both sides of the old aspect-ratio threshold, same-size maps, maps larger than the base, one-texel dimensions, nonlinear metadata and padded strides. A 4×2 base with a 2×2 horizontal gain ramp provides a simple anchor: the sample at x=1 should be 0.25, rather than 0.5. Seven focused tests passed, including the legacy gain-map-output decode regression and one existing encode/decode parameter. Separate same-size comparisons produced matching output checksums for all three map formats with linear, HLG and PQ output.

Local measurements used matched macOS arm64 Release builds with intrinsics enabled and the same JPEG library, with six timed samples per version/case in balanced order. Controlled 12 MP JPEG files with grayscale gain maps decoded in 50.56 ms before and 48.70 ms after at 2× reduction, and 49.26 ms before and 48.90 ms after at 4×. The ranges overlap, so these show no measured whole-decode regression in those cases rather than an established speedup. The isolated gain-application benchmark still showed a 4× cost: +7.5% median wall time and +6.8% process CPU time; 2× was approximately neutral. The stage benchmark uses an RGBA base, while JPEG decoding uses its decoded image layout, so the stage and whole-decode results are separate measurements. File reads, output checksums and decoder cleanup are outside the whole-decode timing; decoder creation and setup are included. Other platforms remain unmeasured.

With intrinsics enabled, the three existing Apple tests passed on both baseline and candidate, including decoding both old and new gain-map fixtures. The candidate's two focused gain-application tests also passed. These Apple checks cover successful decoding and metadata handling, not a reference HDR pixel comparison.

A small local visual check compared the Apple window scene, a night scene and a photographic collage, using identical linear-output rendering and amplified difference images. The two latter original files have full-size gain maps and matched exactly. Controlled 2× and 4× reduced-map variants exercised the changed path: decoded base pixels were preserved, metadata matched within 1e−5, and the reduced maps were JPEG-encoded at quality 100. Inspection of the tone-mapped previews found no obvious new halos or gross gradient failures. This is limited regression screening, not HDR ground-truth validation or proof of better visual quality.

Live GLES comparison, HDR-display evaluation and native Windows testing remain outstanding. This changes shared CPU reconstruction behavior and should be reviewed as a sampling-policy change, rather than an output-preserving optimization.

The two focused gain-application tests also passed ASan and UBSan with the core, image I/O library and test translation unit instrumented. JPEG and GoogleTest were prebuilt dependencies. The full test suite and HEIF/AVIF integration were not run.

Sample gain maps with independent axis scales and centered bilinear filtering. Preserve direct reads for same-size maps and avoid the intermediate byte resize for mismatched aspect ratios.

Add analytic sampling coverage for grayscale, RGB, RGBA, dimension mismatches, boundaries, and padded strides.
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.

1 participant