Blender V5.3
node_composite_hue_correct.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2006 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BLI_math_base.hh"
6#include "BLI_math_color_c.hh"
7#include "BLI_math_vector.hh"
9
11
12#include "NOD_multi_function.hh"
13
14#include "BKE_colortools.hh"
15#include "BKE_node.hh"
16
17#include "GPU_material.hh"
18
19#include "COM_result.hh"
20
22
24
26{
27 b.is_function_node();
28 b.add_input<decl::Color>("Image"_ustr).default_value({1.0f, 1.0f, 1.0f, 1.0f});
29 b.add_input<decl::Float>("Factor"_ustr, "Fac"_ustr)
30 .default_value(1.0f)
31 .min(0.0f)
32 .max(1.0f)
34 b.add_output<decl::Color>("Image"_ustr);
35}
36
37static void node_init(bNodeTree * /*ntree*/, bNode *node)
38{
39 node->storage = BKE_curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
40
41 CurveMapping *cumapping = static_cast<CurveMapping *>(node->storage);
42
43 cumapping->preset = CURVE_PRESET_MID8;
44
45 for (int c = 0; c < 3; c++) {
46 CurveMap *cuma = &cumapping->cm[c];
47 BKE_curvemap_reset(cuma, &cumapping->clipr, cumapping->preset, CurveMapSlopeType::Positive);
48 }
49 /* use wrapping for all hue correct nodes */
50 cumapping->flag |= CUMA_USE_WRAPPING;
51 /* default to showing Saturation */
52 cumapping->cur = 1;
53}
54
55using namespace blender::compositor;
56
58{
59 return static_cast<CurveMapping *>(node.storage);
60}
61
62static int node_gpu_material(GPUMaterial *material,
63 bNode *node,
64 bNodeExecData * /*execdata*/,
65 GPUNodeStack *inputs,
66 GPUNodeStack *outputs)
67{
68 CurveMapping *curve_mapping = get_curve_mapping(*node);
69
70 BKE_curvemapping_init(curve_mapping);
71 float *band_values;
72 int band_size;
73 BKE_curvemapping_table_RGBA(curve_mapping, &band_values, &band_size);
74 float band_layer;
75 GPUNodeLink *band_texture = GPU_color_band(material, band_size, band_values, &band_layer);
76
77 float range_minimums[CM_TOT];
78 BKE_curvemapping_get_range_minimums(curve_mapping, range_minimums);
79 float range_dividers[CM_TOT];
80 BKE_curvemapping_compute_range_dividers(curve_mapping, range_dividers);
81
82 return GPU_stack_link(material,
83 node,
84 "node_composite_hue_correct",
85 inputs,
86 outputs,
87 band_texture,
88 GPU_constant(&band_layer),
89 GPU_uniform(range_minimums),
90 GPU_uniform(range_dividers));
91}
92
93static float4 hue_correct(const float4 &color, const float factor, const CurveMapping *curve_map)
94{
95 float3 hsv;
96 rgb_to_hsv_v(color, hsv);
97
98 /* We parameterize the curve using the hue value. */
99 float parameter = hsv.x;
100
101 /* Adjust each of the Hue, Saturation, and Values accordingly to the following rules. A curve map
102 * value of 0.5 means no change in hue, so adjust the value to get an identity at 0.5. Since the
103 * identity of addition is 0, we subtract 0.5 (0.5 - 0.5 = 0). A curve map value of 0.5 means no
104 * change in saturation or value, so adjust the value to get an identity at 0.5. Since the
105 * identity of multiplication is 1, we multiply by 2 (0.5 * 2 = 1). */
106 hsv.x += BKE_curvemapping_evaluateF(curve_map, 0, parameter) - 0.5f;
107 hsv.y *= BKE_curvemapping_evaluateF(curve_map, 1, parameter) * 2.0f;
108 hsv.z *= BKE_curvemapping_evaluateF(curve_map, 2, parameter) * 2.0f;
109
110 /* Sanitize the new hue and saturation values. */
111 hsv.x = math::fract(hsv.x);
112 hsv.y = math::clamp(hsv.y, 0.0f, 1.0f);
113
114 float3 rgb_result;
115 hsv_to_rgb_v(hsv, rgb_result);
116 float4 result = float4(math::max(rgb_result, float3(0.0f)), color.w);
117
118 return math::interpolate(color, result, factor);
119}
120
122
124{
125 CurveMapping *curve_mapping = get_curve_mapping(builder.node());
126 BKE_curvemapping_init(curve_mapping);
127
129 return mf::build::SI2_SO<Color, float, Color>(
130 "Hue Correct",
131 /* Take ownership of the tree because it contains the curve mapping. */
132 [curve_mapping, tree = builder.shared_tree()](const Color &color, const float factor)
133 -> Color { return Color(hue_correct(float4(color), factor, curve_mapping)); },
134 mf::build::exec_presets::SomeSpanOrSingle<0>());
135 });
136}
137
138static void node_register()
139{
140 static bke::bNodeType ntype;
141
142 cmp_node_type_base(&ntype, "CompositorNodeHueCorrect"_ustr, CMP_NODE_HUECORRECT);
143 ntype.ui_name = "Hue Correct";
144 ntype.ui_description = "Adjust hue, saturation, and value with a curve";
145 ntype.enum_name_legacy = "HUECORRECT";
147 ntype.declare = node_declare;
149 ntype.initfunc = node_init;
153
155}
157
158} // namespace blender::nodes::node_composite_hue_correct_cc
#define NODE_CLASS_OP_COLOR
Definition BKE_node.hh:480
#define CMP_NODE_HUECORRECT
#define CM_TOT
#define NOD_REGISTER_NODE(REGISTER_FUNC)
void construct_and_set_matching_fn_cb(Fn &&create_multi_function)
const std::shared_ptr< const bNodeTree > & shared_tree()
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
void node_type_storage(bNodeType &ntype, std::optional< StringRefNull > storagename, void(*freefunc)(bNode *node), void(*copyfunc)(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node))
Definition node.cc:6166
ColorSceneLinear4f< eAlpha::Premultiplied > Color
Definition COM_result.hh:96
T clamp(const T &a, const T &min, const T &max)
T interpolate(const T &a, const T &b, const FactorT &t)
T fract(const T &a)
T max(const T &a, const T &b)
static float4 hue_correct(const float4 &color, const float factor, const CurveMapping *curve_map)
static CurveMapping * get_curve_mapping(const bNode &node)
static int node_gpu_material(GPUMaterial *material, bNode *node, bNodeExecData *, GPUNodeStack *inputs, GPUNodeStack *outputs)
static void node_build_multi_function(nodes::NodeMultiFunctionBuilder &builder)
static void node_declare(NodeDeclarationBuilder &b)
CurveMapping * BKE_curvemapping_add(int tot, float minx, float miny, float maxx, float maxy)
Definition colortools.cc:99
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
void rgb_to_hsv_v(const float rgb[3], float r_hsv[3])
GPUNodeLink * GPU_color_band(GPUMaterial *mat, int size, float *pixels, float *r_row)
GPUNodeLink * GPU_uniform(const float *num)
void BKE_curvemapping_init(CurveMapping *cumap)
GPUNodeLink * GPU_constant(const float *num)
void hsv_to_rgb_v(const float hsv[3], float r_rgb[3])
Definition math_color.cc:61
void node_free_curves(bNode *node)
Definition node_util.cc:40
void BKE_curvemap_reset(CurveMap *cuma, const rctf *clipr, int preset, CurveMapSlopeType slope)
void node_copy_curves(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:52
void BKE_curvemapping_table_RGBA(const CurveMapping *cumap, float **array, int *size)
void BKE_curvemapping_compute_range_dividers(const CurveMapping *curve_mapping, float dividers[4])
@ PROP_FACTOR
Definition RNA_types.hh:308
void BKE_curvemapping_get_range_minimums(const CurveMapping *curve_mapping, float minimums[4])
float BKE_curvemapping_evaluateF(const CurveMapping *cumap, int cur, float value)
void cmp_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
eCurveMappingFlags flag
eCurveMappingPreset preset
static constexpr int _320
Definition BKE_node.hh:251
Defines a node type.
Definition BKE_node.hh:265
std::string ui_description
Definition BKE_node.hh:271
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:373
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:382
void(*) initfunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:320
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393