Blender V5.0
node_composite_huecorrect.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
8
9#include "BLI_math_base.hh"
10#include "BLI_math_color.h"
11#include "BLI_math_vector.hh"
13
15
16#include "NOD_multi_function.hh"
17
18#include "BKE_colortools.hh"
19#include "BKE_node.hh"
20
21#include "GPU_material.hh"
22
24
26
28{
29 b.is_function_node();
30 b.add_input<decl::Color>("Image").default_value({1.0f, 1.0f, 1.0f, 1.0f});
31 b.add_input<decl::Float>("Factor", "Fac")
32 .default_value(1.0f)
33 .min(0.0f)
34 .max(1.0f)
36 b.add_output<decl::Color>("Image");
37}
38
39static void node_composit_init_huecorrect(bNodeTree * /*ntree*/, bNode *node)
40{
41 node->storage = BKE_curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
42
43 CurveMapping *cumapping = (CurveMapping *)node->storage;
44
45 cumapping->preset = CURVE_PRESET_MID8;
46
47 for (int c = 0; c < 3; c++) {
48 CurveMap *cuma = &cumapping->cm[c];
49 BKE_curvemap_reset(cuma, &cumapping->clipr, cumapping->preset, CurveMapSlopeType::Positive);
50 }
51 /* use wrapping for all hue correct nodes */
52 cumapping->flag |= CUMA_USE_WRAPPING;
53 /* default to showing Saturation */
54 cumapping->cur = 1;
55}
56
57using namespace blender::compositor;
58
60{
61 return static_cast<CurveMapping *>(node.storage);
62}
63
64static int node_gpu_material(GPUMaterial *material,
65 bNode *node,
66 bNodeExecData * /*execdata*/,
69{
70 CurveMapping *curve_mapping = get_curve_mapping(*node);
71
72 BKE_curvemapping_init(curve_mapping);
73 float *band_values;
74 int band_size;
75 BKE_curvemapping_table_RGBA(curve_mapping, &band_values, &band_size);
76 float band_layer;
77 GPUNodeLink *band_texture = GPU_color_band(material, band_size, band_values, &band_layer);
78
79 float range_minimums[CM_TOT];
80 BKE_curvemapping_get_range_minimums(curve_mapping, range_minimums);
81 float range_dividers[CM_TOT];
82 BKE_curvemapping_compute_range_dividers(curve_mapping, range_dividers);
83
84 return GPU_stack_link(material,
85 node,
86 "node_composite_hue_correct",
87 inputs,
88 outputs,
89 band_texture,
90 GPU_constant(&band_layer),
91 GPU_uniform(range_minimums),
92 GPU_uniform(range_dividers));
93}
94
95static float4 hue_correct(const float4 &color, const float factor, const CurveMapping *curve_map)
96{
97 float3 hsv;
98 rgb_to_hsv_v(color, hsv);
99
100 /* We parameterize the curve using the hue value. */
101 float parameter = hsv.x;
102
103 /* Adjust each of the Hue, Saturation, and Values accordingly to the following rules. A curve map
104 * value of 0.5 means no change in hue, so adjust the value to get an identity at 0.5. Since the
105 * identity of addition is 0, we subtract 0.5 (0.5 - 0.5 = 0). A curve map value of 0.5 means no
106 * change in saturation or value, so adjust the value to get an identity at 0.5. Since the
107 * identity of multiplication is 1, we multiply by 2 (0.5 * 2 = 1). */
108 hsv.x += BKE_curvemapping_evaluateF(curve_map, 0, parameter) - 0.5f;
109 hsv.y *= BKE_curvemapping_evaluateF(curve_map, 1, parameter) * 2.0f;
110 hsv.z *= BKE_curvemapping_evaluateF(curve_map, 2, parameter) * 2.0f;
111
112 /* Sanitize the new hue and saturation values. */
113 hsv.x = math::fract(hsv.x);
114 hsv.y = math::clamp(hsv.y, 0.0f, 1.0f);
115
116 float3 rgb_result;
117 hsv_to_rgb_v(hsv, rgb_result);
118 float4 result = float4(math::max(rgb_result, float3(0.0f)), color.w);
119
120 return math::interpolate(color, result, factor);
121}
122
124{
125 CurveMapping *curve_mapping = get_curve_mapping(builder.node());
126 BKE_curvemapping_init(curve_mapping);
127
129 return mf::build::SI2_SO<float4, float, float4>(
130 "Hue Correct",
131 [=](const float4 &color, const float factor) -> float4 {
132 return hue_correct(color, factor, curve_mapping);
133 },
134 mf::build::exec_presets::SomeSpanOrSingle<0>());
135 });
136}
137
138} // namespace blender::nodes::node_composite_huecorrect_cc
139
141{
143
144 static blender::bke::bNodeType ntype;
145
146 cmp_node_type_base(&ntype, "CompositorNodeHueCorrect", CMP_NODE_HUECORRECT);
147 ntype.ui_name = "Hue Correct";
148 ntype.ui_description = "Adjust hue, saturation, and value with a curve";
149 ntype.enum_name_legacy = "HUECORRECT";
151 ntype.declare = file_ns::cmp_node_huecorrect_declare;
152 blender::bke::node_type_size(ntype, 320, 140, 500);
153 ntype.initfunc = file_ns::node_composit_init_huecorrect;
155 ntype.gpu_fn = file_ns::node_gpu_material;
156 ntype.build_multi_function = file_ns::node_build_multi_function;
157
159}
void BKE_curvemapping_compute_range_dividers(const CurveMapping *curve_mapping, float dividers[4])
void BKE_curvemapping_get_range_minimums(const CurveMapping *curve_mapping, float minimums[4])
void BKE_curvemap_reset(CurveMap *cuma, const rctf *clipr, int preset, CurveMapSlopeType slope)
float BKE_curvemapping_evaluateF(const CurveMapping *cumap, int cur, float value)
void BKE_curvemapping_init(CurveMapping *cumap)
CurveMapping * BKE_curvemapping_add(int tot, float minx, float miny, float maxx, float maxy)
Definition colortools.cc:89
void BKE_curvemapping_table_RGBA(const CurveMapping *cumap, float **array, int *size)
#define NODE_CLASS_OP_COLOR
Definition BKE_node.hh:449
#define CMP_NODE_HUECORRECT
void hsv_to_rgb_v(const float hsv[3], float r_rgb[3])
Definition math_color.cc:57
void rgb_to_hsv_v(const float rgb[3], float r_hsv[3])
#define CM_TOT
@ CUMA_USE_WRAPPING
@ CURVE_PRESET_MID8
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
GPUNodeLink * GPU_constant(const float *num)
GPUNodeLink * GPU_color_band(GPUMaterial *mat, int size, float *pixels, float *r_row)
GPUNodeLink * GPU_uniform(const float *num)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
@ PROP_FACTOR
Definition RNA_types.hh:251
void construct_and_set_matching_fn_cb(Fn &&create_multi_function)
void node_type_size(bNodeType &ntype, int width, int minwidth, int maxwidth)
Definition node.cc:5384
void node_register_type(bNodeType &ntype)
Definition node.cc:2416
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:5414
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 void node_composit_init_huecorrect(bNodeTree *, bNode *node)
static int node_gpu_material(GPUMaterial *material, bNode *node, bNodeExecData *, GPUNodeStack *inputs, GPUNodeStack *outputs)
static void cmp_node_huecorrect_declare(NodeDeclarationBuilder &b)
static CurveMapping * get_curve_mapping(const bNode &node)
static void node_build_multi_function(blender::nodes::NodeMultiFunctionBuilder &builder)
static float4 hue_correct(const float4 &color, const float factor, const CurveMapping *curve_map)
VecBase< float, 4 > float4
VecBase< float, 3 > float3
static void register_node_type_cmp_huecorrect()
void cmp_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
static blender::bke::bNodeSocketTemplate outputs[]
static blender::bke::bNodeSocketTemplate inputs[]
void node_copy_curves(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:49
void node_free_curves(bNode *node)
Definition node_util.cc:37
CurveMap cm[4]
void * storage
Defines a node type.
Definition BKE_node.hh:238
std::string ui_description
Definition BKE_node.hh:244
void(* initfunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:289
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:342
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:351
const char * enum_name_legacy
Definition BKE_node.hh:247
NodeDeclareFunction declare
Definition BKE_node.hh:362