Skip to content

test(color): measure the 16-bit encoder's gamma stage and resolution (#4326) - #4365

Merged
zackees merged 1 commit into
masterfrom
test/4326-wide-output-resolution
Sep 11, 2026
Merged

zackees merged 1 commit into
masterfrom
test/4326-wide-output-resolution

Conversation

@zackees

@zackees zackees commented Sep 11, 2026 •

Copy link
Copy Markdown
Member

Measures #4326 end to end, which is the piece that issue names as the first work: "I have not measured this end to end ... the claim should be confirmed by it before the fix is judged." Does not close it — the fix (a no-gamma wide path in the 16-bit encoders) is untouched, and HD108 still has no settings in scope at all.

Both halves of #4326 turn out to be the same number.

The managed drive is 8-bit quantized, then gamma-encoded

kProfile gives all three emitters luminance 1.0 at sRGB chromaticities; source is linear sRGB; dither off. Full red is then a target of Y = 0.2126 — sRGB red's luminance share — against an emitter making Y = 1.0 at full drive, so the device solve's answer is a red drive of 0.2126. That is correct, and it is where the pipeline's job ends.

red, 16-bit
what a wide encoder should emit for drive 0.2126 13933
what goes on the wire 849
unbound, same pixel 65535
bound, black 0

849 decodes exactly: 0.2126 × 255 = 54, then (54/255)^2.8 × 65535 = 849. The drive is narrowed to 8 bits before the encoder sees it, and the gamma-2.8 stage B1/§6 forbid after the device solve is applied on top. 16.4× too dark, from a value_or(2.8f) default rather than anything a user asked for.

The resolution cost, quantified

Sweeping all 256 source codes on the unbound path — where 8-bit-plus-gamma is the legacy behaviour and correct, so this is the encoder's own resolution rather than a managed-mode defect:

  • 252 distinct 16-bit values of 65536 — four pairs collide in the gamma LUT;
  • endpoints reached (top = 65535), so the loss is resolution inside the range, not a squeezed scale;
  • widest neighbouring step: 717 counts, so 716 of every 717 representable values are unreachable near full scale.

Verification

Changed settings.mGamma.value_or(2.8f) to value_or(1.0f) — roughly the fix #4326 proposes — and the gamma case fails, 1 of 19. It leaves the resolution case untouched, because there mGamma is set explicitly so value_or never fires. That asymmetry is itself the confirmation that the managed path is the one running on the default.

Assertions are stated as the shortfall as well as the literal (linear_of_same_drive > red16 * 10), so a failure says what is wrong rather than that a number moved.

These are written to fail when #4326 is fixed

Deliberately. A wide-fed encoder would emit near 13933, and the cases say so in their comments. Recording the current behaviour is what the issue asked for before the fix is judged.

Local: bash lint clean; full C++ suite green.

Test-only; no src/ change.

🤖 Generated with Claude Code

https://claude.ai/code/session_01MLhWkMfzLjrnLTDMBE6Fj9

Summary by CodeRabbit

  • Tests
    • Added coverage for UCS7604 channel encoding with bound profiles, including expected red and black output values.
    • Added validation of unbound channel encoder resolution across all 256 source codes.
    • Documented a known encoding behavior that is expected to change when the related issue is resolved.

…4326)

#4326 says plainly that it had not been measured: "I have not measured this
end to end ... the claim should be confirmed by it before the fix is judged."
Measured now, and both halves of the issue turn out to be one number.

`kProfile` gives all three emitters luminance 1.0 at sRGB chromaticities and
the source is linear sRGB, so full red is a target of Y = 0.2126 against an
emitter making Y = 1.0 at full drive. The device solve's answer is a red drive
of 0.2126, which is correct and is where the pipeline's job ends. A 16-bit
encoder consuming that drive should put 13933 on the wire. It puts 849.

849 decodes exactly: the drive quantized to 8 bits is 0.2126 * 255 = 54, and
(54/255)^2.8 * 65535 = 849. So the drive is narrowed to 8 bits before the
encoder sees it, and then the gamma stage B1/§6 forbid after the device solve
is applied on top of it -- 16.4x too dark, from a `value_or(2.8f)` default
rather than anything a user asked for.

Confirmed causally rather than by arithmetic alone: changing that default to
1.0f fails the case, and leaves the resolution case untouched because there
`mGamma` is set explicitly so `value_or` never fires. That asymmetry is the
confirmation that the managed path is the one running on the default.

The second case puts a number on "at most 256 distinct levels out of 65536".
Sweeping all 256 source codes on the unbound path, where 8-bit-plus-gamma is
the legacy behaviour and correct: 252 distinct values, four pairs colliding in
the LUT, endpoints reached so the range is used, and a widest neighbouring
step of 717 counts -- 716 of every 717 representable values unreachable near
full scale.

Both cases are written to fail when #4326 is fixed, and say so. A wide-fed
encoder would emit near 13933. Recording the current behaviour is what the
issue asked for before the fix is judged; the fix itself is untouched, and
HD108 still has no `settings` in scope at all.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01MLhWkMfzLjrnLTDMBE6Fj9
@coderabbitai

coderabbitai Bot commented Sep 11, 2026 •

Copy link
Copy Markdown

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Advanced

Run ID: 01f6497b-f52b-40fd-8ff0-d2bb2ea05780

📥 Commits

Reviewing files that changed from the base of the PR and between ae32271 and 1ea1008.

📒 Files selected for processing (1)
  • tests/fl/channels/channel.cpp

Included review availability: Your plan provides up to 8 included reviews per hour; 0 remain after this review.


📝 Walkthrough

Walkthrough

Adds two UCS7604 channel tests. One verifies managed-path gamma output for red and black. The other measures distinct 16-bit outputs and neighbor spacing across all 256 unbound source codes.

Changes

UCS7604 encoder tests

Layer / File(s) Summary
Encoder behavior coverage
tests/fl/channels/channel.cpp
The tests verify managed-path outputs of 849 for full red and 0 for black. They also verify 252 distinct unbound 16-bit levels, a maximum of 65535, and a maximum neighbor step of 717.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Other · Severity of issue fixed: Medium

Merge Risk: ⚪ Minimal · up to 1ea10

The added encoder coverage is deterministic and introduces no remaining actionable merge risk.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 1 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the added tests for the 16-bit encoder gamma stage and resolution. It matches the pull request objectives and references issue #4326.
Linked Issues check ✅ Passed The PR meets the coding objective in issue #4326 to measure the existing encoder behavior before the fix. tests/fl/channels/channel.cpp adds an end-to-end bound-profile test that records the managed…
Out of Scope Changes check ✅ Passed The diff adds only two tests in tests/fl/channels/channel.cpp. The tests directly measure the managed gamma behavior and native 16-bit encoder resolution required by #4326. No unrelated source chang…
  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch test/4326-wide-output-resolution

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@zackees
zackees merged commit 049161c into master Sep 11, 2026
6 checks passed
@fastled-project-sync fastled-project-sync Bot moved this from Triage to Done in FastLED Tracker Sep 11, 2026
zackees added a commit that referenced this pull request Sep 12, 2026
#4326) (#4368)

* feat(color): let the 16-bit encoder consume the device drive (P8, #4042/#4326)

A colour-managed UCS7604-16 channel put 849 on the wire where the device solve
asked for 13933. #4365 measured that and pinned it; this is the fix, and the
same pixel now encodes as 13935 -- within two counts of the drive, which is
the s16.16 value and `quantize16` each rounding once.

849 was the drive narrowed to 8 bits (0.2126 * 255 = 54) and widened again by
a gamma-2.8 LUT. Two defects in one number: the RGB8 round trip B3 forbids on
the managed path, and a second shaping stage after the device solve that
B1/§6 forbid outright -- from `mGamma.value_or(2.8f)`, a default rather than
anything a caller asked for, since `setColorProfile` clears `mGamma`.

The fix is upstream of where #4326 located it. A no-gamma path fed by
`loadAndScaleRGB` would still be 8-bit; `PixelIterator` had no wider primitive
to offer, which its own comment already said:

    Doing that properly needs a primitive that loads the pixel scaled at full
    brightness, which PixelIterator does not expose today.

So: `PixelIterator::loadAndScaleRGB16`, with `PixelController` widening its
8-bit pixel exactly -- byte-identical to what the RGB16 adapter did before, so
unbound channels are untouched -- and `ColorManagedPixelSource` quantizing its
s16.16 drives to 16 bits instead of to 8 and back up. Then a wide UCS7604
encoder that writes those bytes with no shaping at all, selected when a
profile is bound.

Guarded by `!FL_PLATFORM_HAS_TINY_MEMORY`, which is what
`FL_COLOR_PROFILE_RUNTIME` expands to, spelled directly rather than including
a channels header from the encoders layer. That is the trade the two
`FASTLED_HD_COLOR_MIXING` pointers in the same file already make: one function
pointer per PixelIterator on targets that can bind a profile, nothing on a
tiny one.

Not claimed: RGBW stays on the gamma path, because `ColorManagedPixelSource`
delegates its RGBW entry point to the legacy controller and there is no wide
drive to consume yet. And for an 8-bit CRGB source this buys accuracy, not
more reachable levels -- 256 inputs still give at most 256 outputs. The level
count improves when the source itself is wide.

#4365's two cases were written to fail when this landed, and they did. The
gamma one is now the statement of the fix: it bounds the emitted value to the
drive and asserts it is specifically not 849, so a regression restoring the
gamma stage cannot satisfy the bounds by coincidence. The resolution case is
unchanged -- it measures the unbound path, where 8-bit-plus-gamma is the
legacy behaviour and correct.

bash lint clean, 305/305 unit and 84/84 examples.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01MLhWkMfzLjrnLTDMBE6Fj9

* perf(color): one range and one encoder for the UCS7604 16-bit path

The bloat gate caught this: +1,120 B on an ESP32-S3 Blink build that binds no
profile, against a 342,483 B baseline. Branching between a gamma range and a
wide range instantiates both, and a sketch that never binds a profile carries
the second for nothing. Raising the baseline for that would be paying flash on
every target for a path most of them never take.

Folded together instead. The input is always the 16-bit range, and the gamma
case recovers the byte its LUT wants: `map8_to_16(b) >> 8 == b` exactly, since
`b * 257 >> 8` is `b` for every `b <= 255`. So the unbound path is
byte-identical to the 8-bit range it replaces, and `gamma == nullptr` is the
colour-managed case where the source has already quantized once to 16 bits.

`encodeUCS7604_16bit_RGB` now takes `const Gamma8*` rather than a reference.
It has one caller.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01MLhWkMfzLjrnLTDMBE6Fj9

* fix(color): select the wide path from the iterator, not the options

Review finding, and a real defect rather than a style point.

`wide_source` was `settings.hasColorProfile()`, which answers "is a profile
configured" -- not "was the managed iterator built". Those differ:
`ReorderingPixelIteratorAny` only constructs the managed iterator when
`pipeline != nullptr && hooks.makeIterator != nullptr`, so a rejected binding,
or a build where the pipeline seam is not linked, leaves the legacy controller
in place while the options still report a profile.

In that state the old code took the no-gamma path over legacy 8-bit pixels,
dropping a stage that path still needs. It now asks the iterator, through a
new `isManaged()`, which is the state that actually decides which pixels
arrive.

Also replaces the section sign in two comments with ASCII, and adds the
FL_NO_EXCEPT the ratchet wants on the changed signature.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01MLhWkMfzLjrnLTDMBE6Fj9

* perf(color): read wide pixels straight off the iterator, no second adapter

Second attempt at the bloat gate. Folding the two ranges into one took
+1,120 B to +988 B, which was the wrong lever: the cost is not the duplicate
branch, it is that routing the 16-bit path through `ScaledPixelIteratorRGB16`
pulls that adapter and its iterator-pair templates into every sketch that
links UCS7604 -- which is every sketch, because `fl::Channel::showPixels`
keeps each `writeUCS7604(...)` statically reachable by design.

So the legacy path goes back to exactly what it was, 8-bit range plus gamma,
and the managed path is a plain loop over `PixelIterator`: `has(1)`,
`loadAndScaleRGB16`, write six bytes, `stepDithering`, `advanceData`. That is
what the adapter does, in the same order, without instantiating it.

A Blink build binds no profile and never enters the wide branch; what it now
carries for this feature is the loop body and the vtable pointer, not a
second iterator adapter.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01MLhWkMfzLjrnLTDMBE6Fj9

* perf(color): bind the wide load only for sources that have one

Measured instead of guessed, and the guesses were wrong. Three blind attempts
took the ESP32-S3 Blink cost from +1,120 B to +940 B; a symbol diff against
master found 750 B of it in one place:

    +125  PixelControllerVtable<PixelController<(EOrder)80,  ...>>::loadAndScaleRGB16
    +125  PixelControllerVtable<PixelController<(EOrder)66,  ...>>::loadAndScaleRGB16
    +125  PixelControllerVtable<PixelController<(EOrder)17,  ...>>::loadAndScaleRGB16
    +125  PixelControllerVtable<PixelController<(EOrder)136, ...>>::loadAndScaleRGB16
    +125  PixelControllerVtable<PixelController<(EOrder)129, ...>>::loadAndScaleRGB16
    +125  PixelControllerVtable<PixelController<(EOrder)10,  ...>>::loadAndScaleRGB16

Six copies of the same thunk, one per colour order, each doing the identical
widening -- because every `PixelController` got a wide entry point whose body
was just `map8_to_16` of its 8-bit load.

Only a colour-managed source has a wider pixel to offer. So `PixelController`
loses the method entirely, `WideLoadBinder` binds the thunk only for a type
that declares one, and the pointer stays null otherwise;
`PixelIterator::loadAndScaleRGB16` then widens in one place for every order
rather than once per order.

+964 B -> +255 B against master, and what is left is the feature itself: the
wide encoder (+157), the `isManaged()` branch in `showPixels` (+63), and
small call-site changes. Those are statically reachable because
`fl::Channel::showPixels` keeps every `writeUCS7604(...)` reachable by design.

Also worth recording: `bash bloat esp32s3 --build` builds `.pio/build/.../
firmware.elf` but analyses `.build/pio/esp32s3/.fbuild/build/release/
firmware.elf`, which was five days stale here -- two runs of it reported
byte-identical output across a real code change. The numbers above come from
`fbuild symbols` on the ELF the build actually produced.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01MLhWkMfzLjrnLTDMBE6Fj9

* chore(bloat): record the 231 B the wide 16-bit path costs (#4326)

The gate asks for an upward move to read like a decision in git log, so the
entry carries the symbol diff rather than a number: +157 B for the wide
encoder, +63 B for the `isManaged()` branch in showPixels, +17 B for
PixelIterator::has.

It also records what this is not. The first three attempts cost 1,120 B,
988 B and 940 B; 750 B of that was six copies of one vtable thunk, one per
colour order, each widening an 8-bit pixel identically. Binding the thunk only
for sources that declare a wide load removed all six. 231 B is what is left
after that, not what the naive version cost.

The residue is statically reachable rather than elidable because
`fl::Channel::showPixels` keeps every `writeUCS7604(...)` reference live so the
encoder bodies link -- which that switch already documents.
`-DFASTLED_DISABLE_UCS7604=1` drops it with the rest of the chipset, and a
tiny-memory target pays 0 B.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01MLhWkMfzLjrnLTDMBE6Fj9

* fix(color): guard the wide 16-bit encoder for tiny-memory builds

- Guard `encodeUCS7604_16bit_RGB_wide` and its `wide_source` call site
  with `#if !FL_PLATFORM_HAS_TINY_MEMORY`. `PixelIterator::loadAndScaleRGB16`
  is compiled out on parts with <=1KB SRAM, and `pixels` is a concrete
  `PixelIterator&` rather than a dependent type, so the call is bound at
  template *definition* time -- the body alone was a hard compile error on
  TINY even though no caller ever selected it. Verified: forcing
  FL_PLATFORM_HAS_TINY_MEMORY=1 over the header failed to compile before
  this change and compiles clean after, with the gamma fallback retained.
- Retire the now-unused `wide_source` parameter on TINY via FL_UNUSED.
- Replace the last two non-ASCII characters in the file (micro sign, em
  dash) with ASCII, per the ASCII-only rule for C++ sources.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01MLhWkMfzLjrnLTDMBE6Fj9

---------

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
@zackees
zackees deleted the test/4326-wide-output-resolution branch November 5, 2026 05:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Native 16-bit encoders (HD108, UCS7604-16) are fed 8 bits and widened by a gamma LUT

1 participant