Blender V5.3
node_composite_channel_key.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"
8
10
11#include "NOD_multi_function.hh"
13
14#include "BKE_node.hh"
15
16#include "GPU_material.hh"
17
18#include "COM_result.hh"
19
21
23
25 {CMP_NODE_CHANNEL_MATTE_CS_RGB, "RGB", 0, N_("RGB"), N_("RGB (Red, Green, Blue) color space")},
27 "HSV",
28 0,
29 N_("HSV"),
30 N_("HSV (Hue, Saturation, Value) color space")},
32 "YUV",
33 0,
34 N_("YUV"),
35 N_("YUV (Y - luma, U V - chroma) color space")},
37 "YCC",
38 0,
39 N_("YCbCr"),
40 N_("YCbCr (Y - luma, Cb - blue-difference chroma, Cr - red-difference chroma) color space")},
41 {0, nullptr, 0, nullptr, nullptr},
42};
43
44enum class RGBChannel : uint8_t {
45 R = 0,
46 G = 1,
47 B = 2,
48};
49
51 {int(RGBChannel::R), "R", 0, "R", ""},
52 {int(RGBChannel::G), "G", 0, "G", ""},
53 {int(RGBChannel::B), "B", 0, "B", ""},
54 {0, nullptr, 0, nullptr, nullptr},
55};
56
57enum class HSVChannel : uint8_t {
58 H = 0,
59 S = 1,
60 V = 2,
61};
62
64 {int(HSVChannel::H), "H", 0, "H", ""},
65 {int(HSVChannel::S), "S", 0, "S", ""},
66 {int(HSVChannel::V), "V", 0, "V", ""},
67 {0, nullptr, 0, nullptr, nullptr},
68};
69
70enum class YUVChannel : uint8_t {
71 Y = 0,
72 U = 1,
73 V = 2,
74};
75
77 {int(YUVChannel::Y), "Y", 0, "Y", ""},
78 {int(YUVChannel::U), "U", 0, "U", ""},
79 {int(YUVChannel::V), "V", 0, "V", ""},
80 {0, nullptr, 0, nullptr, nullptr},
81};
82
83enum class YCbCrChannel : uint8_t {
84 Y = 0,
85 Cb = 1,
86 Cr = 2,
87};
88
90 {int(YCbCrChannel::Y), "Y", 0, "Y", ""},
91 {int(YCbCrChannel::Cb), "CB", 0, "Cb", ""},
92 {int(YCbCrChannel::Cr), "CR", 0, "Cr", ""},
93 {0, nullptr, 0, nullptr, nullptr},
94};
95
98 "SINGLE",
99 0,
100 N_("Single"),
101 N_("Limit by single channel")},
103 "MAX",
104 0,
105 N_("Max"),
106 N_("Limit by maximum of other channels")},
107 {0, nullptr, 0, nullptr, nullptr},
108};
109
111{
112 b.use_custom_socket_order();
113 b.allow_any_socket_order();
114 b.is_function_node();
115 b.add_input<decl::Color>("Image"_ustr).default_value({1.0f, 1.0f, 1.0f, 1.0f}).hide_value();
116 b.add_output<decl::Color>("Image"_ustr).align_with_previous();
117 b.add_output<decl::Float>("Matte"_ustr);
118
119 b.add_input<decl::Float>("Minimum"_ustr)
120 .default_value(0.0f)
122 .min(0.0f)
123 .max(1.0f)
124 .description("Channel values lower than this minimum are keyed");
125 b.add_input<decl::Float>("Maximum"_ustr)
126 .default_value(1.0f)
128 .min(0.0f)
129 .max(1.0f)
130 .description("Channel values higher than this maximum are not keyed");
131
132 b.add_input<decl::Menu>("Color Space"_ustr)
133 .default_value(CMP_NODE_CHANNEL_MATTE_CS_RGB)
134 .static_items(color_space_items)
135 .expanded()
137 b.add_input<decl::Menu>("RGB Key Channel"_ustr)
138 .default_value(RGBChannel::G)
139 .static_items(rgb_channel_items)
140 .expanded()
142 .usage_by_menu("Color Space"_ustr, CMP_NODE_CHANNEL_MATTE_CS_RGB)
143 .optional_label();
144 b.add_input<decl::Menu>("HSV Key Channel"_ustr)
145 .default_value(HSVChannel::H)
146 .static_items(hsv_channel_items)
147 .expanded()
149 .usage_by_menu("Color Space"_ustr, CMP_NODE_CHANNEL_MATTE_CS_HSV)
150 .optional_label();
151 b.add_input<decl::Menu>("YUV Key Channel"_ustr)
152 .default_value(YUVChannel::V)
153 .static_items(yuv_channel_items)
154 .expanded()
155 .usage_by_menu("Color Space"_ustr, CMP_NODE_CHANNEL_MATTE_CS_YUV)
157 b.add_input<decl::Menu>("YCbCr Key Channel"_ustr)
158 .default_value(YCbCrChannel::Cr)
159 .static_items(ycbcr_channel_items)
160 .expanded()
161 .usage_by_menu("Color Space"_ustr, CMP_NODE_CHANNEL_MATTE_CS_YCC)
163
164 b.add_input<decl::Menu>("Limit Method"_ustr)
166 .static_items(limit_method_items)
167 .expanded()
169 b.add_input<decl::Menu>("RGB Limit Channel"_ustr)
170 .default_value(RGBChannel::R)
171 .static_items(rgb_channel_items)
172 .expanded()
174 .make_available([](bNode &node) {
175 bNodeSocket &limit_method_socket = *bke::node_find_socket(
176 node, SOCK_IN, "Limit Method"_ustr);
177 limit_method_socket.default_value_typed<bNodeSocketValueMenu>()->value =
179
180 bNodeSocket &color_space_socket = *bke::node_find_socket(
181 node, SOCK_IN, "Color Space"_ustr);
182 color_space_socket.default_value_typed<bNodeSocketValueMenu>()->value =
184 })
185 .usage_inference(
186 [](const socket_usage_inference::SocketUsageParams &params) -> std::optional<bool> {
187 return params.menu_input_may_be("Limit Method"_ustr,
189 params.menu_input_may_be("Color Space"_ustr, CMP_NODE_CHANNEL_MATTE_CS_RGB);
190 });
191 b.add_input<decl::Menu>("HSV Limit Channel"_ustr)
192 .default_value(HSVChannel::S)
193 .static_items(hsv_channel_items)
194 .expanded()
196 .make_available([](bNode &node) {
197 bNodeSocket &limit_method_socket = *bke::node_find_socket(
198 node, SOCK_IN, "Limit Method"_ustr);
199 limit_method_socket.default_value_typed<bNodeSocketValueMenu>()->value =
201
202 bNodeSocket &color_space_socket = *bke::node_find_socket(
203 node, SOCK_IN, "Color Space"_ustr);
204 color_space_socket.default_value_typed<bNodeSocketValueMenu>()->value =
206 })
207 .usage_inference(
208 [](const socket_usage_inference::SocketUsageParams &params) -> std::optional<bool> {
209 return params.menu_input_may_be("Limit Method"_ustr,
211 params.menu_input_may_be("Color Space"_ustr, CMP_NODE_CHANNEL_MATTE_CS_HSV);
212 });
213 b.add_input<decl::Menu>("YUV Limit Channel"_ustr)
214 .default_value(YUVChannel::U)
215 .static_items(yuv_channel_items)
216 .expanded()
218 .make_available([](bNode &node) {
219 bNodeSocket &limit_method_socket = *bke::node_find_socket(
220 node, SOCK_IN, "Limit Method"_ustr);
221 limit_method_socket.default_value_typed<bNodeSocketValueMenu>()->value =
223
224 bNodeSocket &color_space_socket = *bke::node_find_socket(
225 node, SOCK_IN, "Color Space"_ustr);
226 color_space_socket.default_value_typed<bNodeSocketValueMenu>()->value =
228 })
229 .usage_inference(
230 [](const socket_usage_inference::SocketUsageParams &params) -> std::optional<bool> {
231 return params.menu_input_may_be("Limit Method"_ustr,
233 params.menu_input_may_be("Color Space"_ustr, CMP_NODE_CHANNEL_MATTE_CS_YUV);
234 });
235 b.add_input<decl::Menu>("YCbCr Limit Channel"_ustr)
236 .default_value(YCbCrChannel::Cb)
237 .static_items(ycbcr_channel_items)
238 .expanded()
240 .make_available([](bNode &node) {
241 bNodeSocket &limit_method_socket = *bke::node_find_socket(
242 node, SOCK_IN, "Limit Method"_ustr);
243 limit_method_socket.default_value_typed<bNodeSocketValueMenu>()->value =
245
246 bNodeSocket &color_space_socket = *bke::node_find_socket(
247 node, SOCK_IN, "Color Space"_ustr);
248 color_space_socket.default_value_typed<bNodeSocketValueMenu>()->value =
250 })
251 .usage_inference(
252 [](const socket_usage_inference::SocketUsageParams &params) -> std::optional<bool> {
253 return params.menu_input_may_be("Limit Method"_ustr,
255 params.menu_input_may_be("Color Space"_ustr, CMP_NODE_CHANNEL_MATTE_CS_YCC);
256 });
257}
258
259static void node_init(bNodeTree * /*ntree*/, bNode *node)
260{
261 /* Unused, but allocated for forward compatibility. */
262 node->storage = MEM_new<NodeChroma>(__func__);
263}
264
265using namespace blender::compositor;
266
267static int node_gpu_material(GPUMaterial *material,
268 bNode *node,
269 bNodeExecData * /*execdata*/,
270 GPUNodeStack *inputs,
271 GPUNodeStack *outputs)
272{
273 return GPU_stack_link(material, node, "node_composite_channel_matte", inputs, outputs);
274}
275
277{
278 switch (color_space) {
280 return color.xyz();
281 }
283 float4 hsv;
284 rgb_to_hsv_v(color, hsv);
285 return hsv.xyz();
286 }
288 float4 yuv;
289 rgb_to_yuv(color.x, color.y, color.z, &yuv.x, &yuv.y, &yuv.z, BLI_YUV_ITU_BT709);
290 return yuv.xyz();
291 }
293 float4 ycc;
294 rgb_to_ycc(color.x, color.y, color.z, &ycc.x, &ycc.y, &ycc.z, BLI_YCC_ITU_BT709);
295 ycc /= 255.0f;
296 return ycc.xyz();
297 }
298 }
299
300 return color.xyz();
301}
302
304 const int rgb_channel,
305 const int hsv_channel,
306 const int yuv_channel,
307 const int ycc_channel)
308{
309 switch (color_space) {
311 return rgb_channel;
313 return hsv_channel;
315 return yuv_channel;
317 return ycc_channel;
318 }
319
320 return 0;
321}
322
323/* Compute the indices of the channels used to compute the limit value. We always assume the limit
324 * algorithm is Max, if it is a single limit channel, store it in both limit channels, because
325 * the maximum of two identical values is the same value. */
327 const int matte_channel,
328 const int limit_channel)
329{
330 /* If the algorithm is Max, store the indices of the other two channels other than the matte
331 * channel. */
332 if (limit_method == CMP_NODE_CHANNEL_MATTE_LIMIT_ALGORITHM_MAX) {
333 return int2((matte_channel + 1) % 3, (matte_channel + 2) % 3);
334 }
335
336 /* If the algorithm is Single, store the index of the limit channel in both channels. */
337 return int2(limit_channel);
338}
339
340static void channel_key(const float4 color,
341 const float minimum,
342 const float maximum,
343 const CMPNodeChannelMatteColorSpace color_space,
344 const int rgb_key_channel,
345 const int hsv_key_channel,
346 const int yuv_key_channel,
347 const int ycc_key_channel,
348 const CMPNodeChannelMatteLimitAlgorithm limit_method,
349 const int rgb_limit_channel,
350 const int hsv_limit_channel,
351 const int yuv_limit_channel,
352 const int ycc_limit_channel,
353 float4 &output_color,
354 float &matte)
355{
356 const float3 channels = compute_channels(color, color_space);
357 const int matte_channel = get_channel_index(
358 color_space, rgb_key_channel, hsv_key_channel, yuv_key_channel, ycc_key_channel);
359 const int limit_channel = get_channel_index(
360 color_space, rgb_limit_channel, hsv_limit_channel, yuv_limit_channel, ycc_limit_channel);
361 const int2 limit_channels = compute_limit_channels(limit_method, matte_channel, limit_channel);
362
363 float matte_value = channels[matte_channel];
364 float limit_value = math::max(channels[limit_channels.x], channels[limit_channels.y]);
365
366 float alpha = 1.0f - (matte_value - limit_value);
367 if (alpha > maximum) {
368 alpha = color.w;
369 }
370 else if (alpha < minimum) {
371 alpha = 0.0f;
372 }
373 else {
374 alpha = (alpha - minimum) / (maximum - minimum);
375 }
376
377 matte = math::min(alpha, color.w);
378 output_color = color * matte;
379}
380
382
384{
385 static auto function =
386 mf::build::detail::build_multi_function_with_n_inputs_two_outputs<Color, float>(
387 "Channel Key",
388 [=](const Color &color,
389 const float &minimum,
390 const float &maximum,
391 const MenuValue &color_space,
392 const MenuValue &rgb_key_channel,
393 const MenuValue &hsv_key_channel,
394 const MenuValue &yuv_key_channel,
395 const MenuValue &ycc_key_channel,
396 const MenuValue &limit_method,
397 const MenuValue &rgb_limit_channel,
398 const MenuValue &hsv_limit_channel,
399 const MenuValue &yuv_limit_channel,
400 const MenuValue &ycc_limit_channel,
401 Color &output_color,
402 float &matte) -> void {
403 float4 out_color;
405 minimum,
406 maximum,
408 rgb_key_channel.value,
409 hsv_key_channel.value,
410 yuv_key_channel.value,
411 ycc_key_channel.value,
413 rgb_limit_channel.value,
414 hsv_limit_channel.value,
415 yuv_limit_channel.value,
416 ycc_limit_channel.value,
417 out_color,
418 matte);
419 output_color = Color(out_color);
420 },
421 mf::build::exec_presets::SomeSpanOrSingle<0>(),
423 float,
424 float,
425 MenuValue,
426 MenuValue,
427 MenuValue,
428 MenuValue,
429 MenuValue,
430 MenuValue,
431 MenuValue,
432 MenuValue,
433 MenuValue,
434 MenuValue>());
435
436 builder.set_matching_fn(function);
437}
438
439static void node_register()
440{
441 static bke::bNodeType ntype;
442
443 cmp_node_type_base(&ntype, "CompositorNodeChannelMatte"_ustr, CMP_NODE_CHANNEL_MATTE);
444 ntype.ui_name = "Channel Key";
445 ntype.ui_description = "Create matte based on differences in color channels";
446 ntype.enum_name_legacy = "CHANNEL_MATTE";
447 ntype.nclass = NODE_CLASS_MATTE;
448 ntype.declare = node_declare;
449 ntype.flag |= NODE_PREVIEW;
450 ntype.initfunc = node_init;
455
457}
459
460} // namespace blender::nodes::node_composite_channel_key_cc
#define NODE_CLASS_MATTE
Definition BKE_node.hh:485
#define CMP_NODE_CHANNEL_MATTE
#define BLI_YUV_ITU_BT709
#define BLI_YCC_ITU_BT709
#define BLT_I18NCONTEXT_COLOR
#define Y
#define NOD_REGISTER_NODE(REGISTER_FUNC)
void set_matching_fn(const mf::MultiFunction *fn)
std::optional< std::string > translation_context
bool menu_input_may_be(UString identifier, int enum_value) const
#define B
#define R
#define H(x, y, z)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
bNodeSocket * node_find_socket(bNode &node, eNodeSocketInOut in_out, UString identifier)
Definition node.cc:3036
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 min(const T &a, const T &b)
T max(const T &a, const T &b)
static void node_build_multi_function(nodes::NodeMultiFunctionBuilder &builder)
static int node_gpu_material(GPUMaterial *material, bNode *node, bNodeExecData *, GPUNodeStack *inputs, GPUNodeStack *outputs)
static float3 compute_channels(const float4 color, const CMPNodeChannelMatteColorSpace color_space)
static int get_channel_index(const CMPNodeChannelMatteColorSpace color_space, const int rgb_channel, const int hsv_channel, const int yuv_channel, const int ycc_channel)
static void node_declare(NodeDeclarationBuilder &b)
static int2 compute_limit_channels(const CMPNodeChannelMatteLimitAlgorithm limit_method, const int matte_channel, const int limit_channel)
static void channel_key(const float4 color, const float minimum, const float maximum, const CMPNodeChannelMatteColorSpace color_space, const int rgb_key_channel, const int hsv_key_channel, const int yuv_key_channel, const int ycc_key_channel, const CMPNodeChannelMatteLimitAlgorithm limit_method, const int rgb_limit_channel, const int hsv_limit_channel, const int yuv_limit_channel, const int ycc_limit_channel, float4 &output_color, float &matte)
UserDef U
Definition blender.cc:58
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])
Global G
Definition blender.cc:57
void rgb_to_ycc(float r, float g, float b, float *r_y, float *r_cb, float *r_cr, int colorspace)
CMPNodeChannelMatteColorSpace
@ CMP_NODE_CHANNEL_MATTE_CS_HSV
@ CMP_NODE_CHANNEL_MATTE_CS_YUV
@ CMP_NODE_CHANNEL_MATTE_CS_RGB
@ CMP_NODE_CHANNEL_MATTE_CS_YCC
unsigned char uint8_t
Definition BKE_text.h:79
void rgb_to_yuv(float r, float g, float b, float *r_y, float *r_u, float *r_v, int colorspace)
Definition math_color.cc:71
CMPNodeChannelMatteLimitAlgorithm
@ CMP_NODE_CHANNEL_MATTE_LIMIT_ALGORITHM_SINGLE
@ CMP_NODE_CHANNEL_MATTE_LIMIT_ALGORITHM_MAX
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:57
void node_free_standard_storage(bNode *node)
Definition node_util.cc:45
@ PROP_FACTOR
Definition RNA_types.hh:308
void cmp_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
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
#define N_(msgid)
CCL_NAMESPACE_BEGIN struct Window V