Blender V4.5
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_interface.hh"
22#include "UI_resources.hh"
23
24#include "GPU_material.hh"
25
27
28/* ******************* Chroma Key ********************************************************** */
29
31
33{
34 b.add_input<decl::Color>("Image").default_value({1.0f, 1.0f, 1.0f, 1.0f});
35 b.add_input<decl::Color>("Key Color").default_value({1.0f, 1.0f, 1.0f, 1.0f});
36 b.add_input<decl::Float>("Minimum")
37 .default_value(DEG2RADF(10.0f))
39 .description(
40 "If the angle between the color and the key color in CrCb space is less than this "
41 "minimum angle, it is keyed");
42 b.add_input<decl::Float>("Maximum")
43 .default_value(DEG2RADF(30.0f))
45 .description(
46 "If the angle between the color and the key color in CrCb space is larger than this "
47 "maximum angle, it is not keyed");
48 b.add_input<decl::Float>("Falloff")
49 .default_value(1.0f)
50 .min(0.0f)
51 .max(1.0f)
53 .description(
54 "Controls the falloff between keyed and non-keyed values. 0 means completely sharp and "
55 "1 means completely smooth");
56
57 b.add_output<decl::Color>("Image");
58 b.add_output<decl::Float>("Matte");
59}
60
61using namespace blender::compositor;
62
63static int node_gpu_material(GPUMaterial *material,
64 bNode *node,
65 bNodeExecData * /*execdata*/,
68{
69 return GPU_stack_link(material, node, "node_composite_chroma_matte", inputs, outputs);
70}
71
72/* Algorithm from the book Video Demystified. Chapter 7. Chroma Keying. */
73static void chroma_matte(const float4 &color,
74 const float4 &key,
75 const float &minimum,
76 const float &maximum,
77 const float &falloff,
79 float &matte)
80{
81 float3 color_ycca;
83 color.x, color.y, color.z, &color_ycca.x, &color_ycca.y, &color_ycca.z, BLI_YCC_ITU_BT709);
84 color_ycca /= 255.0f;
85 float3 key_ycca;
86 rgb_to_ycc(key.x, key.y, key.z, &key_ycca.x, &key_ycca.y, &key_ycca.z, BLI_YCC_ITU_BT709);
87 key_ycca /= 255.0f;
88
89 /* Normalize the CrCb components into the [-1, 1] range. */
90 float2 color_cc = color_ycca.yz() * 2.0f - 1.0f;
91 float2 key_cc = math::normalize(key_ycca.yz() * 2.0f - 1.0f);
92
93 /* Rotate the color onto the space of the key such that x axis of the color space passes
94 * through the key color. */
95 color_cc = math::from_direction(key_cc * float2(1.0f, -1.0f)) * color_cc;
96
97 /* Compute foreground key. If positive, the value is in the [0, 1] range. */
98 float foreground_key = color_cc.x - (math::abs(color_cc.y) / math::tan(maximum / 2.0f));
99
100 /* Negative foreground key values retain the original alpha. Positive values are scaled by the
101 * falloff, while colors that make an angle less than the minimum angle get a zero alpha. */
102 float alpha = color.w;
103 if (foreground_key > 0.0f) {
104 alpha = 1.0f - (foreground_key / falloff);
105
106 if (math::abs(math::atan2(color_cc.y, color_cc.x)) < (minimum / 2.0f)) {
107 alpha = 0.0f;
108 }
109 }
110
111 /* Compute output. */
112 matte = math::min(alpha, color.w);
113 result = color * matte;
114}
115
117{
119 return mf::build::SI5_SO2<float4, float4, float, float, float, float4, float>(
120 "Chroma Key",
121 [=](const float4 &color,
122 const float4 &key_color,
123 const float &minimum,
124 const float &maximum,
125 const float &falloff,
126 float4 &output_color,
127 float &matte) -> void {
128 chroma_matte(color, key_color, minimum, maximum, falloff, output_color, matte);
129 },
130 mf::build::exec_presets::SomeSpanOrSingle<0, 1>());
131 });
132}
133
134} // namespace blender::nodes::node_composite_chroma_matte_cc
135
137{
139
140 static blender::bke::bNodeType ntype;
141
142 cmp_node_type_base(&ntype, "CompositorNodeChromaMatte", CMP_NODE_CHROMA_MATTE);
143 ntype.ui_name = "Chroma Key";
144 ntype.ui_description = "Create matte based on chroma values";
145 ntype.enum_name_legacy = "CHROMA_MATTE";
146 ntype.nclass = NODE_CLASS_MATTE;
147 ntype.declare = file_ns::cmp_node_chroma_matte_declare;
148 ntype.flag |= NODE_PREVIEW;
149 ntype.gpu_fn = file_ns::node_gpu_material;
150 ntype.build_multi_function = file_ns::node_build_multi_function;
151
153}
#define NODE_CLASS_MATTE
Definition BKE_node.hh:440
#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:240
@ PROP_FACTOR
Definition RNA_types.hh:239
void construct_and_set_matching_fn_cb(Fn &&create_multi_function)
void node_register_type(bNodeType &ntype)
Definition node.cc:2748
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:226
std::string ui_description
Definition BKE_node.hh:232
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