CaptureMoment 0.1.0
Free and open-source non-destructive photo editor
Loading...
Searching...
No Matches
operation_constrast.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_contrast,
18 const Halide::Var& x,
19 const Halide::Var& y,
20 const Halide::Var& c)
21{
22 Halide::Func contrast_func("contrast_op");
23
24 // Safety: Clamp the input parameter to the operation's defined valid range.
25 Halide::Expr safe_contrast = clamp(
26 param_contrast,
29 );
30
31 // Multiplicative contrast centered at 0.5 (Mid-gray in Oklab L-channel)
32 // Formula: 0.5 + (L - 0.5) * ContrastFactor
33 //
34 // IMPORTANT: We do NOT clamp the result of this operation here.
35 // Clamping L inside Oklab while 'a' and 'b' are non-zero causes severe hue shifts
36 // during the oklab_to_rgb conversion. Values > 1.0 or < 0.0 are safely handled
37 // later when clamping the final RGB output.
38 contrast_func(x, y, c) = select(
39 c == 0,
40 0.5f + (input(x, y, c) - 0.5f) * safe_contrast,
41 input(x, y, c) // a, b, and Alpha channels remain untouched
42 );
43
44 return contrast_func;
45}
46
47// ============================================================================
48// IOperationFusionLogic Implementation
49// ============================================================================
50
52 const Halide::Func& input_func,
53 const Halide::Var& x,
54 const Halide::Var& y,
55 const Halide::Var& c,
56 const Halide::Param<float>& param
57 ) const
58{
59 // Halide's optimizer will simplify the math if the contrast factor is 1.0.
60 spdlog::trace("OperationContrast::appendToFusedPipeline: Fusing with Halide Param (In-Graph Clamped)");
61 return applyContrastAdjustment(input_func, param, x, y, c);
62}
63
64} // 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_CONTRAST_VALUE
Minimum allowed contrast value. Defined by OperationRanges::getContrastMinValue().
static constexpr float MAX_CONTRAST_VALUE
Maximum allowed contrast value. Defined by OperationRanges::getContrastMaxValue().
Halide::Func applyContrastAdjustment(const InputType &input, const Halide::Param< float > &param_contrast, const Halide::Var &x, const Halide::Var &y, const Halide::Var &c)
Concrete implementation of Contrast adjustment.
CaptureMoment — footer