Blender V5.3
node_composite_chroma_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"
7#include "BLI_math_matrix.hh"
9
11
12#include "NOD_multi_function.hh"
13
14#include "GPU_material.hh"
15
16#include "COM_result.hh"
17
19
21
23{
24 b.use_custom_socket_order();
25 b.allow_any_socket_order();
26 b.is_function_node();
27 b.add_input<decl::Color>("Image"_ustr).default_value({1.0f, 1.0f, 1.0f, 1.0f}).hide_value();
28 b.add_output<decl::Color>("Image"_ustr).align_with_previous();
29 b.add_output<decl::Float>("Matte"_ustr);
30
31 b.add_input<decl::Color>("Key Color"_ustr).default_value({1.0f, 1.0f, 1.0f, 1.0f});
32 b.add_input<decl::Float>("Minimum"_ustr)
33 .default_value(DEG2RADF(10.0f))
35 .description(
36 "If the angle between the color and the key color in CrCb space is less than this "
37 "minimum angle, it is keyed");
38 b.add_input<decl::Float>("Maximum"_ustr)
39 .default_value(DEG2RADF(30.0f))
41 .description(
42 "If the angle between the color and the key color in CrCb space is larger than this "
43 "maximum angle, it is not keyed");
44 b.add_input<decl::Float>("Falloff"_ustr)
45 .default_value(1.0f)
46 .min(0.0f)
47 .max(1.0f)
49 .description(
50 "Controls the falloff between keyed and non-keyed values. 0 means completely sharp and "
51 "1 means completely smooth");
52}
53
54using namespace blender::compositor;
55
56static int node_gpu_material(GPUMaterial *material,
57 bNode *node,
58 bNodeExecData * /*execdata*/,
59 GPUNodeStack *inputs,
60 GPUNodeStack *outputs)
61{
62 return GPU_stack_link(material, node, "node_composite_chroma_matte", inputs, outputs);
63}
64
65/* Algorithm from the book Video Demystified. Chapter 7. Chroma Keying. */
66static void chroma_matte(const float4 &color,
67 const float4 &key,
68 const float &minimum,
69 const float &maximum,
70 const float &falloff,
72 float &matte)
73{
74 float3 color_ycca;
76 color.x, color.y, color.z, &color_ycca.x, &color_ycca.y, &color_ycca.z, BLI_YCC_ITU_BT709);
77 color_ycca /= 255.0f;
78 float3 key_ycca;
79 rgb_to_ycc(key.x, key.y, key.z, &key_ycca.x, &key_ycca.y, &key_ycca.z, BLI_YCC_ITU_BT709);
80 key_ycca /= 255.0f;
81
82 /* Normalize the CrCb components into the [-1, 1] range. */
83 float2 color_cc = color_ycca.yz() * 2.0f - 1.0f;
84 float2 key_cc = math::normalize(key_ycca.yz() * 2.0f - 1.0f);
85
86 /* Rotate the color onto the space of the key such that x axis of the color space passes
87 * through the key color. */
88 color_cc = math::from_direction(key_cc * float2(1.0f, -1.0f)) * color_cc;
89
90 /* Compute foreground key. If positive, the value is in the [0, 1] range. */
91 float foreground_key = color_cc.x - (math::abs(color_cc.y) / math::tan(maximum / 2.0f));
92
93 /* Negative foreground key values retain the original alpha. Positive values are scaled by the
94 * falloff, while colors that make an angle less than the minimum angle get a zero alpha. */
95 float alpha = color.w;
96 if (foreground_key > 0.0f) {
97 alpha = 1.0f - (foreground_key / falloff);
98
99 if (math::abs(math::atan2(color_cc.y, color_cc.x)) < (minimum / 2.0f)) {
100 alpha = 0.0f;
101 }
102 }
103
104 /* Compute output. */
105 matte = math::min(alpha, color.w);
106 result = color * matte;
107}
108
110
112{
114 return mf::build::SI5_SO2<Color, Color, float, float, float, Color, float>(
115 "Chroma Key",
116 [=](const Color &color,
117 const Color &key_color,
118 const float &minimum,
119 const float &maximum,
120 const float &falloff,
121 Color &output_color,
122 float &matte) -> void {
123 float4 out_color;
125 float4(color), float4(key_color), minimum, maximum, falloff, out_color, matte);
126 output_color = Color(out_color);
127 },
128 mf::build::exec_presets::SomeSpanOrSingle<0, 1>());
129 });
130}
131
132static void node_register()
133{
134 static bke::bNodeType ntype;
135
136 cmp_node_type_base(&ntype, "CompositorNodeChromaMatte"_ustr, CMP_NODE_CHROMA_MATTE);
137 ntype.ui_name = "Chroma Key";
138 ntype.ui_description = "Create matte based on chroma values";
139 ntype.enum_name_legacy = "CHROMA_MATTE";
140 ntype.nclass = NODE_CLASS_MATTE;
141 ntype.declare = node_declare;
142 ntype.flag |= NODE_PREVIEW;
146
148}
150
151} // namespace blender::nodes::node_composite_chroma_key_cc
#define NODE_CLASS_MATTE
Definition BKE_node.hh:485
#define CMP_NODE_CHROMA_MATTE
#define BLI_YCC_ITU_BT709
#define DEG2RADF(_deg)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
void construct_and_set_matching_fn_cb(Fn &&create_multi_function)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
ColorSceneLinear4f< eAlpha::Premultiplied > Color
Definition COM_result.hh:96
T min(const T &a, const T &b)
MatBase< T, 2, 2 > from_direction(const VecBase< T, 2 > &direction)
T atan2(const T &y, const T &x)
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
T abs(const T &a)
T tan(const AngleRadianBase< T > &a)
static void node_build_multi_function(nodes::NodeMultiFunctionBuilder &builder)
static int node_gpu_material(GPUMaterial *material, bNode *node, bNodeExecData *, GPUNodeStack *inputs, GPUNodeStack *outputs)
static void node_declare(NodeDeclarationBuilder &b)
static void chroma_matte(const float4 &color, const float4 &key, const float &minimum, const float &maximum, const float &falloff, float4 &result, float &matte)
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
void rgb_to_ycc(float r, float g, float b, float *r_y, float *r_cb, float *r_cr, int colorspace)
@ PROP_FACTOR
Definition RNA_types.hh:308
void cmp_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
static constexpr int _160
Definition BKE_node.hh:246
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
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393