Blender V4.5
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 "BKE_node.hh"
10#include "BLI_math_base.hh"
11#include "BLI_math_color.h"
12#include "BLI_math_vector.hh"
14
16
17#include "NOD_multi_function.hh"
18
19#include "BKE_colortools.hh"
20
21#include "GPU_material.hh"
22
24
26
28{
29 b.add_input<decl::Float>("Fac")
30 .default_value(1.0f)
31 .min(0.0f)
32 .max(1.0f)
34 .compositor_domain_priority(1);
35 b.add_input<decl::Color>("Image")
36 .default_value({1.0f, 1.0f, 1.0f, 1.0f})
37 .compositor_domain_priority(0);
38 b.add_output<decl::Color>("Image");
39}
40
41static void node_composit_init_huecorrect(bNodeTree * /*ntree*/, bNode *node)
42{
43 node->storage = BKE_curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
44
45 CurveMapping *cumapping = (CurveMapping *)node->storage;
46
47 cumapping->preset = CURVE_PRESET_MID8;
48
49 for (int c = 0; c < 3; c++) {
50 CurveMap *cuma = &cumapping->cm[c];
51 BKE_curvemap_reset(cuma, &cumapping->clipr, cumapping->preset, CURVEMAP_SLOPE_POSITIVE);
52 }
53 /* use wrapping for all hue correct nodes */
54 cumapping->flag |= CUMA_USE_WRAPPING;
55 /* default to showing Saturation */
56 cumapping->cur = 1;
57}
58
59using namespace blender::compositor;
60
62{
63 return static_cast<CurveMapping *>(node.storage);
64}
65
66static int node_gpu_material(GPUMaterial *material,
67 bNode *node,
68 bNodeExecData * /*execdata*/,
71{
72 CurveMapping *curve_mapping = get_curve_mapping(*node);
73
74 BKE_curvemapping_init(curve_mapping);
75 float *band_values;
76 int band_size;
77 BKE_curvemapping_table_RGBA(curve_mapping, &band_values, &band_size);
78 float band_layer;
79 GPUNodeLink *band_texture = GPU_color_band(material, band_size, band_values, &band_layer);
80
81 float range_minimums[CM_TOT];
82 BKE_curvemapping_get_range_minimums(curve_mapping, range_minimums);
83 float range_dividers[CM_TOT];
84 BKE_curvemapping_compute_range_dividers(curve_mapping, range_dividers);
85
86 return GPU_stack_link(material,
87 node,
88 "node_composite_hue_correct",
89 inputs,
90 outputs,
91 band_texture,
92 GPU_constant(&band_layer),
93 GPU_uniform(range_minimums),
94 GPU_uniform(range_dividers));
95}
96
97static float4 hue_correct(const float factor, const float4 &color, const CurveMapping *curve_map)
98{
99 float3 hsv;
100 rgb_to_hsv_v(color, hsv);
101
102 /* We parameterize the curve using the hue value. */
103 float parameter = hsv.x;
104
105 /* Adjust each of the Hue, Saturation, and Values accordingly to the following rules. A curve map
106 * value of 0.5 means no change in hue, so adjust the value to get an identity at 0.5. Since the
107 * identity of addition is 0, we subtract 0.5 (0.5 - 0.5 = 0). A curve map value of 0.5 means no
108 * change in saturation or value, so adjust the value to get an identity at 0.5. Since the
109 * identity of multiplication is 1, we multiply by 2 (0.5 * 2 = 1). */
110 hsv.x += BKE_curvemapping_evaluateF(curve_map, 0, parameter) - 0.5f;
111 hsv.y *= BKE_curvemapping_evaluateF(curve_map, 1, parameter) * 2.0f;
112 hsv.z *= BKE_curvemapping_evaluateF(curve_map, 2, parameter) * 2.0f;
113
114 /* Sanitize the new hue and saturation values. */
115 hsv.x = math::fract(hsv.x);
116 hsv.y = math::clamp(hsv.y, 0.0f, 1.0f);
117
118 float3 rgb_result;
119 hsv_to_rgb_v(hsv, rgb_result);
120 float4 result = float4(math::max(rgb_result, float3(0.0f)), color.w);
121
122 return math::interpolate(color, result, factor);
123}
124
126{
127 CurveMapping *curve_mapping = get_curve_mapping(builder.node());
128 BKE_curvemapping_init(curve_mapping);
129
131 return mf::build::SI2_SO<float, float4, float4>(
132 "Hue Correct",
133 [=](const float factor, const float4 &color) -> float4 {
134 return hue_correct(factor, color, curve_mapping);
135 },
136 mf::build::exec_presets::SomeSpanOrSingle<1>());
137 });
138}
139
140} // namespace blender::nodes::node_composite_huecorrect_cc
141
143{
145
146 static blender::bke::bNodeType ntype;
147
148 cmp_node_type_base(&ntype, "CompositorNodeHueCorrect", CMP_NODE_HUECORRECT);
149 ntype.ui_name = "Hue Correct";
150 ntype.ui_description = "Adjust hue, saturation, and value with a curve";
151 ntype.enum_name_legacy = "HUECORRECT";
153 ntype.declare = file_ns::cmp_node_huecorrect_declare;
154 blender::bke::node_type_size(ntype, 320, 140, 500);
155 ntype.initfunc = file_ns::node_composit_init_huecorrect;
157 ntype.gpu_fn = file_ns::node_gpu_material;
158 ntype.build_multi_function = file_ns::node_build_multi_function;
159
161}
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])
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_curvemap_reset(CurveMap *cuma, const rctf *clipr, int preset, int slope)
void BKE_curvemapping_table_RGBA(const CurveMapping *cumap, float **array, int *size)
@ CURVEMAP_SLOPE_POSITIVE
#define NODE_CLASS_OP_COLOR
Definition BKE_node.hh:435
#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])
@ 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:239
void construct_and_set_matching_fn_cb(Fn &&create_multi_function)
#define CM_TOT
void node_type_size(bNodeType &ntype, int width, int minwidth, int maxwidth)
Definition node.cc:5573
void node_register_type(bNodeType &ntype)
Definition node.cc:2748
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:5603
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 float4 hue_correct(const float factor, const float4 &color, const CurveMapping *curve_map)
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)
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:226
std::string ui_description
Definition BKE_node.hh:232
void(* initfunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:277
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:330
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:344
const char * enum_name_legacy
Definition BKE_node.hh:235
NodeDeclareFunction declare
Definition BKE_node.hh:355