CaptureMoment 0.1.0
Free and open-source non-destructive photo editor
Loading...
Searching...
No Matches
operation_whites.cpp
Go to the documentation of this file.
1
9
10#include <spdlog/spdlog.h>
11
13
14template<typename InputType>
16 const InputType& input,
17 const Halide::Param<float>& param_whites,
18 const Halide::Var& x,
19 const Halide::Var& y,
20 const Halide::Var& c)
21{
22 Halide::Func whites_func("whites_op");
23
24 // 1. In Oklab, channel 0 IS the perceptual luminance.
25 Halide::Expr L = input(x, y, 0);
26
27 // 2. Create a smooth mask targeting ONLY the extreme whites.
28 // Unlike Highlights (0.7 - 1.0), Whites should target the very top end (e.g., 0.85 - 1.0)
29 // to adjust clipping without affecting the upper midtones.
30 constexpr float whites_cutoff = 0.85f;
31
32 Halide::Expr t = clamp((L - whites_cutoff) / (1.0f - whites_cutoff), 0.0f, 1.0f);
33
34 // Apply the smoothstep polynomial curve: 3t^2 - 2t^3
35 Halide::Expr mask = t * t * (3.0f - 2.0f * t);
36
37 // 3. Clamp the input parameter to the operation's defined valid range.
38 Halide::Expr safe_val = clamp(
39 param_whites,
42 );
43
44 // 4. Apply adjustment only to channel 0 (L)
45 // NO CLAMP here to prevent hue shifts during oklab_to_rgb conversion.
46 whites_func(x, y, c) = select(
47 c == 0,
48 L + safe_val * mask,
49 input(x, y, c) // a, b, and Alpha channels remain untouched
50 );
51
52 return whites_func;
53}
54
55// ============================================================================
56// IOperationFusionLogic Implementation
57// ============================================================================
58
60 const Halide::Func& input_func,
61 const Halide::Var& x,
62 const Halide::Var& y,
63 const Halide::Var& c,
64 const Halide::Param<float>& param
65 ) const
66{
67 // Halide's constant folding will optimize away additions by zero.
68 spdlog::trace("OperationWhites::appendToFusedPipeline: Fusing with Halide Param (In-Graph Clamped)");
69 return applyWhitesAdjustment(input_func, param, x, y, c);
70}
71
72} // namespace CaptureMoment::Core::Operations
Halide::Func appendToFusedPipeline(const Halide::Func &input_func, const Halide::Var &x, const Halide::Var &y, const Halide::Var &c, const Halide::Param< float > &param) const override
Appends this operation's logic to a fused Halide pipeline. This method is used by the PipelineBuilder...
static constexpr float MIN_WHITES_VALUE
Minimum allowed whites value. Defined by OperationRanges::getWhitesMinValue().
static constexpr float MAX_WHITES_VALUE
Maximum allowed whites value. Defined by OperationRanges::getWhitesMaxValue().
Halide::Func applyWhitesAdjustment(const InputType &input, const Halide::Param< float > &param_whites, const Halide::Var &x, const Halide::Var &y, const Halide::Var &c)
Concrete implementation of Whites adjustment.
CaptureMoment — footer