Blender V5.0
node_composite_chroma_matte.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 <cmath>
10
11#include "BLI_math_base.hh"
12#include "BLI_math_color.h"
13#include "BLI_math_matrix.hh"
14#include "BLI_math_rotation.h"
16
18
19#include "NOD_multi_function.hh"
20
21#include "UI_resources.hh"
22
23#include "GPU_material.hh"
24
26
27/* ******************* Chroma Key ********************************************************** */
28
30
32{
33 b.use_custom_socket_order();
34 b.allow_any_socket_order();
35 b.is_function_node();
36 b.add_input<decl::Color>("Image").default_value({1.0f, 1.0f, 1.0f, 1.0f}).hide_value();
37 b.add_output<decl::Color>("Image").align_with_previous();
38 b.add_output<decl::Float>("Matte");
39
40 b.add_input<decl::Color>("Key Color").default_value({1.0f, 1.0f, 1.0f, 1.0f});
41 b.add_input<decl::Float>("Minimum")
42 .default_value(DEG2RADF(10.0f))
44 .description(
45 "If the angle between the color and the key color in CrCb space is less than this "
46 "minimum angle, it is keyed");
47 b.add_input<decl::Float>("Maximum")
48 .default_value(DEG2RADF(30.0f))
50 .description(
51 "If the angle between the color and the key color in CrCb space is larger than this "
52 "maximum angle, it is not keyed");
53 b.add_input<decl::Float>("Falloff")
54 .default_value(1.0f)
55 .min(0.0f)
56 .max(1.0f)
58 .description(
59 "Controls the falloff between keyed and non-keyed values. 0 means completely sharp and "
60 "1 means completely smooth");
61}
62
63using namespace blender::compositor;
64
65static int node_gpu_material(GPUMaterial *material,
66 bNode *node,
67 bNodeExecData * /*execdata*/,
70{
71 return GPU_stack_link(material, node, "node_composite_chroma_matte", inputs, outputs);
72}
73
74/* Algorithm from the book Video Demystified. Chapter 7. Chroma Keying. */
75static void chroma_matte(const float4 &color,
76 const float4 &key,
77 const float &minimum,
78 const float &maximum,
79 const float &falloff,
81 float &matte)
82{
83 float3 color_ycca;
85 color.x, color.y, color.z, &color_ycca.x, &color_ycca.y, &color_ycca.z, BLI_YCC_ITU_BT709);
86 color_ycca /= 255.0f;
87 float3 key_ycca;
88 rgb_to_ycc(key.x, key.y, key.z, &key_ycca.x, &key_ycca.y, &key_ycca.z, BLI_YCC_ITU_BT709);
89 key_ycca /= 255.0f;
90
91 /* Normalize the CrCb components into the [-1, 1] range. */
92 float2 color_cc = color_ycca.yz() * 2.0f - 1.0f;
93 float2 key_cc = math::normalize(key_ycca.yz() * 2.0f - 1.0f);
94
95 /* Rotate the color onto the space of the key such that x axis of the color space passes
96 * through the key color. */
97 color_cc = math::from_direction(key_cc * float2(1.0f, -1.0f)) * color_cc;
98
99 /* Compute foreground key. If positive, the value is in the [0, 1] range. */
100 float foreground_key = color_cc.x - (math::abs(color_cc.y) / math::tan(maximum / 2.0f));
101
102 /* Negative foreground key values retain the original alpha. Positive values are scaled by the
103 * falloff, while colors that make an angle less than the minimum angle get a zero alpha. */
104 float alpha = color.w;
105 if (foreground_key > 0.0f) {
106 alpha = 1.0f - (foreground_key / falloff);
107
108 if (math::abs(math::atan2(color_cc.y, color_cc.x)) < (minimum / 2.0f)) {
109 alpha = 0.0f;
110 }
111 }
112
113 /* Compute output. */
114 matte = math::min(alpha, color.w);
115 result = color * matte;
116}
117
119{
121 return mf::build::SI5_SO2<float4, float4, float, float, float, float4, float>(
122 "Chroma Key",
123 [=](const float4 &color,
124 const float4 &key_color,
125 const float &minimum,
126 const float &maximum,
127 const float &falloff,
128 float4 &output_color,
129 float &matte) -> void {
130 chroma_matte(color, key_color, minimum, maximum, falloff, output_color, matte);
131 },
132 mf::build::exec_presets::SomeSpanOrSingle<0, 1>());
133 });
134}
135
136} // namespace blender::nodes::node_composite_chroma_matte_cc
137
139{
141
142 static blender::bke::bNodeType ntype;
143
144 cmp_node_type_base(&ntype, "CompositorNodeChromaMatte", CMP_NODE_CHROMA_MATTE);
145 ntype.ui_name = "Chroma Key";
146 ntype.ui_description = "Create matte based on chroma values";
147 ntype.enum_name_legacy = "CHROMA_MATTE";
148 ntype.nclass = NODE_CLASS_MATTE;
149 ntype.declare = file_ns::cmp_node_chroma_matte_declare;
150 ntype.flag |= NODE_PREVIEW;
151 ntype.gpu_fn = file_ns::node_gpu_material;
152 ntype.build_multi_function = file_ns::node_build_multi_function;
154
156}
#define NODE_CLASS_MATTE
Definition BKE_node.hh:454
constexpr int NODE_DEFAULT_MAX_WIDTH
Definition BKE_node.hh:1250
#define CMP_NODE_CHROMA_MATTE
void rgb_to_ycc(float r, float g, float b, float *r_y, float *r_cb, float *r_cr, int colorspace)
#define BLI_YCC_ITU_BT709
#define DEG2RADF(_deg)
@ NODE_PREVIEW
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
@ PROP_ANGLE
Definition RNA_types.hh:252
@ 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
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 cmp_node_chroma_matte_declare(NodeDeclarationBuilder &b)
static int node_gpu_material(GPUMaterial *material, bNode *node, bNodeExecData *, GPUNodeStack *inputs, GPUNodeStack *outputs)
static void node_build_multi_function(blender::nodes::NodeMultiFunctionBuilder &builder)
static void chroma_matte(const float4 &color, const float4 &key, const float &minimum, const float &maximum, const float &falloff, float4 &result, float &matte)
VecBase< float, 4 > float4
VecBase< float, 2 > float2
VecBase< float, 3 > float3
static void register_node_type_cmp_chroma_matte()
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[]
VecBase< T, 2 > yz() const
Defines a node type.
Definition BKE_node.hh:238
std::string ui_description
Definition BKE_node.hh:244
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