Blender V4.5
node_composite_channel_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 "BKE_node.hh"
10#include "BLI_math_base.hh"
11#include "BLI_math_color.h"
13
15
16#include "NOD_multi_function.hh"
17
18#include "RNA_access.hh"
19
20#include "UI_interface.hh"
21#include "UI_resources.hh"
22
23#include "GPU_material.hh"
24
26
27/* ******************* Channel Matte Node ********************************* */
28
30
32
34{
35 b.add_input<decl::Color>("Image").default_value({1.0f, 1.0f, 1.0f, 1.0f});
36 b.add_input<decl::Float>("Minimum")
37 .default_value(0.0f)
38 .subtype(PROP_FACTOR)
39 .min(0.0f)
40 .max(1.0f)
41 .description("Channel values lower than this minimum are keyed");
42 b.add_input<decl::Float>("Maximum")
43 .default_value(1.0f)
44 .subtype(PROP_FACTOR)
45 .min(0.0f)
46 .max(1.0f)
47 .description("Channel values higher than this maximum are not keyed");
48
49 b.add_output<decl::Color>("Image");
50 b.add_output<decl::Float>("Matte");
51}
52
53static void node_composit_init_channel_matte(bNodeTree * /*ntree*/, bNode *node)
54{
56 node->storage = c;
57 c->algorithm = 1; /* Max channel limiting. */
58 c->channel = 1; /* Limit by red. */
59 node->custom1 = 1; /* RGB channel. */
60 node->custom2 = 2; /* Green Channel. */
61}
62
64{
65 uiLayout *col, *row;
66
67 layout->label(IFACE_("Color Space:"), ICON_NONE);
68 row = &layout->row(false);
69 row->prop(
70 ptr, "color_space", UI_ITEM_R_SPLIT_EMPTY_NAME | UI_ITEM_R_EXPAND, std::nullopt, ICON_NONE);
71
72 col = &layout->column(false);
73 col->label(IFACE_("Key Channel:"), ICON_NONE);
74 row = &col->row(false);
75 row->prop(ptr,
76 "matte_channel",
78 std::nullopt,
79 ICON_NONE);
80
81 col = &layout->column(false);
82
83 col->prop(ptr, "limit_method", UI_ITEM_R_SPLIT_EMPTY_NAME, std::nullopt, ICON_NONE);
84 if (RNA_enum_get(ptr, "limit_method") == 0) {
85 col->label(IFACE_("Limiting Channel:"), ICON_NONE);
86 row = &col->row(false);
87 row->prop(ptr,
88 "limit_channel",
90 std::nullopt,
91 ICON_NONE);
92 }
93}
94
95using namespace blender::compositor;
96
98{
99 return static_cast<CMPNodeChannelMatteColorSpace>(node.custom1);
100}
101
102/* Get the index of the channel used to generate the matte. */
103static int get_matte_channel(const bNode &node)
104{
105 return node.custom2 - 1;
106}
107
108/* Get the index of the channel used to compute the limit value. */
109static int get_limit_channel(const bNode &node)
110{
111 return node_storage(node).channel - 1;
112}
113
114/* Get the indices of the channels used to compute the limit value. We always assume the limit
115 * algorithm is Max, if it is a single limit channel, store it in both limit channels, because
116 * the maximum of two identical values is the same value. */
117static int2 get_limit_channels(const bNode &node)
118{
119 int2 limit_channels;
120 if (node_storage(node).algorithm == CMP_NODE_CHANNEL_MATTE_LIMIT_ALGORITHM_MAX) {
121 /* If the algorithm is Max, store the indices of the other two channels other than the matte
122 * channel. */
123 limit_channels[0] = (get_matte_channel(node) + 1) % 3;
124 limit_channels[1] = (get_matte_channel(node) + 2) % 3;
125 }
126 else {
127 /* If the algorithm is Single, store the index of the limit channel in both channels. */
128 limit_channels[0] = get_limit_channel(node);
129 limit_channels[1] = get_limit_channel(node);
130 }
131
132 return limit_channels;
133}
134
135static int node_gpu_material(GPUMaterial *material,
136 bNode *node,
137 bNodeExecData * /*execdata*/,
140{
141 const float color_space = int(get_color_space(*node));
142 const float matte_channel = get_matte_channel(*node);
143 const float2 limit_channels = float2(get_limit_channels(*node));
144
145 return GPU_stack_link(material,
146 node,
147 "node_composite_channel_matte",
148 inputs,
149 outputs,
150 GPU_constant(&color_space),
151 GPU_constant(&matte_channel),
152 GPU_constant(limit_channels));
153}
154
155template<CMPNodeChannelMatteColorSpace ColorSpace>
156static void channel_key(const float4 &color,
157 const float min_limit,
158 const float max_limit,
159 const int matte_channel,
160 const int2 limit_channels,
161 float4 &result,
162 float &matte)
163{
164 float3 channels;
165 if constexpr (ColorSpace == CMP_NODE_CHANNEL_MATTE_CS_HSV) {
166 rgb_to_hsv_v(color, channels);
167 }
170 color.x, color.y, color.z, &channels.x, &channels.y, &channels.z, BLI_YUV_ITU_BT709);
171 }
174 color.x, color.y, color.z, &channels.x, &channels.y, &channels.z, BLI_YCC_ITU_BT709);
175 channels /= 255.0f;
176 }
177 else {
178 channels = color.xyz();
179 }
180
181 float matte_value = channels[matte_channel];
182 float limit_value = math::max(channels[limit_channels.x], channels[limit_channels.y]);
183
184 float alpha = 1.0f - (matte_value - limit_value);
185 if (alpha > max_limit) {
186 alpha = color.w;
187 }
188 else if (alpha < min_limit) {
189 alpha = 0.0f;
190 }
191 else {
192 alpha = (alpha - min_limit) / (max_limit - min_limit);
193 }
194
195 matte = math::min(alpha, color.w);
196 result = color * matte;
197}
198
200{
201 const CMPNodeChannelMatteColorSpace color_space = get_color_space(builder.node());
202 const int matte_channel = get_matte_channel(builder.node());
203 const int2 limit_channels = get_limit_channels(builder.node());
204
205 switch (color_space) {
208 return mf::build::SI3_SO2<float4, float, float, float4, float>(
209 "Channel Key RGB",
210 [=](const float4 &color,
211 const float &minimum,
212 const float &maximum,
213 float4 &output_color,
214 float &matte) -> void {
216 color, minimum, maximum, matte_channel, limit_channels, output_color, matte);
217 },
218 mf::build::exec_presets::SomeSpanOrSingle<0>());
219 });
220 break;
223 return mf::build::SI3_SO2<float4, float, float, float4, float>(
224 "Channel Key HSV",
225 [=](const float4 &color,
226 const float &minimum,
227 const float &maximum,
228 float4 &output_color,
229 float &matte) -> void {
231 color, minimum, maximum, matte_channel, limit_channels, output_color, matte);
232 },
233 mf::build::exec_presets::SomeSpanOrSingle<0>());
234 });
235 break;
238 return mf::build::SI3_SO2<float4, float, float, float4, float>(
239 "Channel Key YUV",
240 [=](const float4 &color,
241 const float &minimum,
242 const float &maximum,
243 float4 &output_color,
244 float &matte) -> void {
246 color, minimum, maximum, matte_channel, limit_channels, output_color, matte);
247 },
248 mf::build::exec_presets::SomeSpanOrSingle<0>());
249 });
250 break;
253 return mf::build::SI3_SO2<float4, float, float, float4, float>(
254 "Channel Key YCC",
255 [=](const float4 &color,
256 const float &minimum,
257 const float &maximum,
258 float4 &output_color,
259 float &matte) -> void {
261 color, minimum, maximum, matte_channel, limit_channels, output_color, matte);
262 },
263 mf::build::exec_presets::SomeSpanOrSingle<0>());
264 });
265 break;
266 }
267}
268
269} // namespace blender::nodes::node_composite_channel_matte_cc
270
272{
274
275 static blender::bke::bNodeType ntype;
276
277 cmp_node_type_base(&ntype, "CompositorNodeChannelMatte", CMP_NODE_CHANNEL_MATTE);
278 ntype.ui_name = "Channel Key";
279 ntype.ui_description = "Create matte based on differences in color channels";
280 ntype.enum_name_legacy = "CHANNEL_MATTE";
281 ntype.nclass = NODE_CLASS_MATTE;
282 ntype.declare = file_ns::cmp_node_channel_matte_declare;
283 ntype.draw_buttons = file_ns::node_composit_buts_channel_matte;
284 ntype.flag |= NODE_PREVIEW;
285 ntype.initfunc = file_ns::node_composit_init_channel_matte;
288 ntype.gpu_fn = file_ns::node_gpu_material;
289 ntype.build_multi_function = file_ns::node_build_multi_function;
290
292}
#define NODE_CLASS_MATTE
Definition BKE_node.hh:440
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1215
#define CMP_NODE_CHANNEL_MATTE
void rgb_to_hsv_v(const float rgb[3], float r_hsv[3])
#define BLI_YUV_ITU_BT709
void rgb_to_ycc(float r, float g, float b, float *r_y, float *r_cb, float *r_cr, int colorspace)
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:67
#define BLI_YCC_ITU_BT709
#define IFACE_(msgid)
@ CMP_NODE_CHANNEL_MATTE_LIMIT_ALGORITHM_MAX
@ NODE_PREVIEW
CMPNodeChannelMatteColorSpace
@ CMP_NODE_CHANNEL_MATTE_CS_YUV
@ CMP_NODE_CHANNEL_MATTE_CS_RGB
@ CMP_NODE_CHANNEL_MATTE_CS_HSV
@ CMP_NODE_CHANNEL_MATTE_CS_YCC
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
GPUNodeLink * GPU_constant(const float *num)
blender::ocio::ColorSpace ColorSpace
#define NOD_REGISTER_NODE(REGISTER_FUNC)
@ PROP_FACTOR
Definition RNA_types.hh:239
@ UI_ITEM_R_SPLIT_EMPTY_NAME
@ UI_ITEM_R_EXPAND
void construct_and_set_matching_fn_cb(Fn &&create_multi_function)
uint col
void * MEM_callocN(size_t len, const char *str)
Definition mallocn.cc:118
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 min(const T &a, const T &b)
T max(const T &a, const T &b)
static int node_gpu_material(GPUMaterial *material, bNode *node, bNodeExecData *, GPUNodeStack *inputs, GPUNodeStack *outputs)
static void node_composit_buts_channel_matte(uiLayout *layout, bContext *, PointerRNA *ptr)
static CMPNodeChannelMatteColorSpace get_color_space(const bNode &node)
static void cmp_node_channel_matte_declare(NodeDeclarationBuilder &b)
static void channel_key(const float4 &color, const float min_limit, const float max_limit, const int matte_channel, const int2 limit_channels, float4 &result, float &matte)
static void node_composit_init_channel_matte(bNodeTree *, bNode *node)
static void node_build_multi_function(blender::nodes::NodeMultiFunctionBuilder &builder)
VecBase< float, 4 > float4
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
VecBase< float, 3 > float3
static void register_node_type_cmp_channel_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[]
void node_free_standard_storage(bNode *node)
Definition node_util.cc:42
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:54
int RNA_enum_get(PointerRNA *ptr, const char *name)
#define min(a, b)
Definition sort.cc:36
int16_t custom1
void * storage
int16_t custom2
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
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:247
NodeDeclareFunction declare
Definition BKE_node.hh:355
void label(blender::StringRef name, int icon)
uiLayout & column(bool align)
uiLayout & row(bool align)
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< blender::StringRef > name_opt, int icon, std::optional< blender::StringRef > placeholder=std::nullopt)
max
Definition text_draw.cc:251
PointerRNA * ptr
Definition wm_files.cc:4227