Skip to content

[refactor] avoid Kelvin/mired round-trip in DNG solver (#273) - #277

Open
thc1006 wants to merge 1 commit into
AcademySoftwareFoundation:mainfrom
thc1006:refactor/issue-273-mired-helper
Open

[refactor] avoid Kelvin/mired round-trip in DNG solver (#273)#277
thc1006 wants to merge 1 commit into
AcademySoftwareFoundation:mainfrom
thc1006:refactor/issue-273-mired-helper

Conversation

@thc1006

@thc1006 thc1006 commented May 9, 2026

Copy link
Copy Markdown

TL;DR

XYZ_to_color_temperature interpolates in mired then converts to Kelvin and
clamps to [2000, 50000]. Its only in-tree caller, find_camera_to_XYZ_matrix,
divides that Kelvin back into mired immediately, producing a redundant
mired -> Kelvin -> mired round-trip flagged by #273.

This PR adds a private helper XYZ_to_mired returning the unclamped mired
value, refactors XYZ_to_color_temperature into a wrapper that preserves the
existing Kelvin signature and clamp, and switches the caller to use the mired
helper directly. Public contract and existing test golden numbers unchanged.

Resolves #273.

What changes

  • src/rawtoaces_core/rawtoaces_core_priv.h: add XYZ_to_mired declaration.
  • src/rawtoaces_core/rawtoaces_core.cpp: split the existing function into
    XYZ_to_mired (the Robertson interpolation, unclamped) plus a two-line
    XYZ_to_color_temperature wrapper that does mired_to_kelvin and the
    [2000, 50000] K clamp. Switch find_camera_to_XYZ_matrix to call the
    mired helper directly.
  • tests/testDNGIdt.cpp: add testIDT_XYZToMired pinning the mired return
    for the same XYZ input as the existing Kelvin test, with a cross-check that
    the wrapper agrees with clamp(mired_to_kelvin(mired), 2000, 50000).

3 files, +48/-12.

Why this shape (and not a rename)

The wrapper is preserved on purpose:

  • The Kelvin clamp [2000, 50000] corresponds exactly to mired anchors
    robertson_mired_table[2] = 20 and robertson_mired_table[26] = 500, and
    1e6/2000 = 500.0 and 1e6/50000 = 20.0 are exact double-precision
    reciprocals. So the wrapper preserves observable behaviour bit-for-bit.
  • All existing testIDT_XYZToColorTemperature* golden numbers continue to
    verify with the same 1e-5 tolerance; no test re-derivation was needed.
  • The Kelvin spelling stays available for any consumer that wants
    user-reportable CCT (EXIF / DNG metadata / light_source_to_color_temp).

If a hard rename to a mired-only API is preferred, it is a one-step
deprecation of the wrapper on top of this PR.

Verification

Built and tested inside aswf/ci-rawtoaces:2026.2 (the same image the VFX
2026 CI lane uses), C++20, conan toolchain, RTA_ENABLE_LENSFUN=ON,
ENABLE_SHARED=ON.

  • Release / gcc 14.2: full ctest 17/17 pass (baseline 17/17, post-change 17/17).
  • Release / clang 19.1: full ctest 17/17 pass.
  • cmake --install + tests/config_tests ctest 2/2 pass.
  • RTA_SANITISER_MODE=address Debug build: 15/15 pass for tests not
    pre-affected by an unrelated lensfun ASan issue
    (Test_LensCorrection, Test_Python_ImageConverter::test_conversion).
    That issue reproduces on main without these changes; trace is
    lfModifier::AddColorCallback allocating 32 B vs ~lfModifier deleting
    24 B inside liblensfun.so.1 (RHEL/Rocky lensfun-0.3.2-15.el8),
    with no rawtoaces frame other than the lens-correction call site.
  • clang-format --dry-run --Werror clean over the entire C++ tree using
    clang-format 16.0.6 (the version pinned by clang-format-check.yml).

Out of scope

  • The inverse color_temperature_to_XYZ has the same Kelvin/mired
    asymmetry. Happy to file a follow-up to symmetrise it.
  • The unrelated CAT target concern raised in [refactor] DNG solver #267 review is untouched.

DCO

Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com>

Copilot AI review requested due to automatic review settings May 9, 2026 13:42
@linux-foundation-easycla

linux-foundation-easycla Bot commented May 9, 2026

Copy link
Copy Markdown

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: thc1006 / name: thc1006 (d2b457f)

…reFoundation#273)

XYZ_to_color_temperature internally interpolates against
robertson_mired_table in mired, then converts to Kelvin and clamps to
[2000, 50000] K. Its only in-tree caller, find_camera_to_XYZ_matrix,
divides that Kelvin result back into mired, producing a redundant
mired -> Kelvin -> mired round-trip flagged by issue AcademySoftwareFoundation#273.

Add a private helper XYZ_to_mired that returns the unclamped mired
value from the same Robertson interpolation. Keep XYZ_to_color_temperature
as a thin wrapper that applies the [2000, 50000] K clamp via
mired_to_kelvin; its public contract and existing test golden numbers
are preserved (the clamp bounds correspond exactly to robertson_mired_table[2]
and robertson_mired_table[26] -- 1e6/2000 = 500.0 and 1e6/50000 = 20.0
are exact double-precision reciprocals).

Switch find_camera_to_XYZ_matrix to call the mired helper directly so
the binary search no longer round-trips through Kelvin.

Add testIDT_XYZToMired pinning the mired return path against the same
XYZ input the existing testIDT_XYZToColorTemperature uses, with a
cross-check that the wrapper still produces the equivalent Kelvin.

Resolves AcademySoftwareFoundation#273.

Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com>
@thc1006
thc1006 force-pushed the refactor/issue-273-mired-helper branch from 2a50053 to d2b457f Compare May 9, 2026 13:55

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

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.

[core] Clarify Kelvin vs mired around XYZ_to_color_temperature (avoid redundant round-trips)

2 participants