CaptureMoment 0.1.0
Free and open-source non-destructive photo editor
Loading...
Searching...
No Matches
operation_shadows.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_shadows,
18 const Halide::Var& x,
19 const Halide::Var& y,
20 const Halide::Var& c)
21{
22 Halide::Func shadows_func("shadows_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 the shadow/lower-midtone range.
28 // Shadows typically cover a wider range than "Blacks" (e.g., 0.0 to 0.5).
29 // Adjust the 0.5f threshold if you want a tighter or wider shadow roll-off.
30 constexpr float shadow_cutoff = 0.5f;
31
32 Halide::Expr t = clamp((shadow_cutoff - L) / shadow_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_shadows,
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 shadows_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 shadows_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 // Zero adjustments are optimized out by Halide internally.
68 spdlog::trace("OperationShadows::appendToFusedPipeline: Fusing with Halide Param (In-Graph Clamped)");
69 return applyShadowsAdjustment(input_func, param, x, y, c);
70}
71
72} // namespace CaptureMoment::Core::Operations
static constexpr float MAX_SHADOWS_VALUE
Maximum allowed shadows value. Defined by OperationRanges::getShadowsMaxValue().
static constexpr float MIN_SHADOWS_VALUE
Minimum allowed shadows value. Defined by OperationRanges::getShadowsMinValue().
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...
Halide::Func applyShadowsAdjustment(const InputType &input, const Halide::Param< float > &param_shadows, const Halide::Var &x, const Halide::Var &y, const Halide::Var &c)
Concrete implementation of Shadows adjustment.
CaptureMoment — footer