Blender V4.5
node_composite_color_spill.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
10
12
13#include "NOD_multi_function.hh"
14
15#include "RNA_access.hh"
16
17#include "UI_interface.hh"
18#include "UI_resources.hh"
19
20#include "GPU_material.hh"
21
23
24/* ******************* Color Spill Suppression ********************************* */
25
27
29
31{
32 b.use_custom_socket_order();
33
34 b.add_output<decl::Color>("Image");
35
36 b.add_layout([](uiLayout *layout, bContext * /*C*/, PointerRNA *ptr) {
37 layout->label(IFACE_("Despill Channel:"), ICON_NONE);
38 uiLayout *row = &layout->row(false);
39 row->prop(
40 ptr, "channel", UI_ITEM_R_SPLIT_EMPTY_NAME | UI_ITEM_R_EXPAND, std::nullopt, ICON_NONE);
41
42 uiLayout *col = &layout->column(false);
43 col->prop(ptr, "limit_method", UI_ITEM_R_SPLIT_EMPTY_NAME, std::nullopt, ICON_NONE);
44
45 if (RNA_enum_get(ptr, "limit_method") == 0) {
46 col->label(IFACE_("Limiting Channel:"), ICON_NONE);
47 row = &col->row(false);
48 row->prop(ptr,
49 "limit_channel",
51 std::nullopt,
52 ICON_NONE);
53 }
54 });
55
56 b.add_input<decl::Color>("Image").default_value({1.0f, 1.0f, 1.0f, 1.0f});
57 b.add_input<decl::Float>("Fac").default_value(1.0f).min(0.0f).max(1.0f).subtype(PROP_FACTOR);
58 b.add_input<decl::Float>("Limit Strength")
59 .default_value(1.0f)
60 .subtype(PROP_FACTOR)
61 .min(0.0f)
62 .max(2.0f)
63 .description("Specifies the limiting strength of the limit channel");
64
65 PanelDeclarationBuilder &use_spill_strength_panel =
66 b.add_panel("Spill Strength").default_closed(true);
67 use_spill_strength_panel.add_input<decl::Bool>("Use Spill Strength")
68 .default_value(false)
69 .panel_toggle()
70 .description(
71 "If enabled, the spill strength for each color channel can be specified. If disabled, "
72 "the spill channel will have a unit scale, while other channels will be zero");
73 use_spill_strength_panel.add_input<decl::Color>("Strength", "Spill Strength")
74 .default_value({0.0f, 1.0f, 0.0f, 1.0f})
75 .description("Specifies the spilling strength of each color channel");
76}
77
78static void node_composit_init_color_spill(bNodeTree * /*ntree*/, bNode *node)
79{
81 node->storage = ncs;
83 node->custom1 = 2; /* green channel */
84 ncs->limchan = 0; /* limit by red */
85}
86
87using namespace blender::compositor;
88
89/* Get the index of the channel used for spilling. */
90static int get_spill_channel(const bNode &node)
91{
92 return node.custom1 - 1;
93}
94
99
100/* Get the index of the channel used for limiting. */
101static int get_limit_channel(const bNode &node)
102{
103 return node_storage(node).limchan;
104}
105
106/* Get the indices of the channels used to compute the limit value. We always assume the limit
107 * algorithm is Average, if it is a single limit channel, store it in both limit channels,
108 * because the average of two identical values is the same value. */
109static int2 get_limit_channels(const bNode &node)
110{
111 int2 limit_channels;
113 /* If the algorithm is Average, store the indices of the other two channels other than the
114 * spill channel. */
115 limit_channels[0] = (get_spill_channel(node) + 1) % 3;
116 limit_channels[1] = (get_spill_channel(node) + 2) % 3;
117 }
118 else {
119 /* If the algorithm is Single, store the index of the limit channel in both channels. */
120 limit_channels[0] = get_limit_channel(node);
121 limit_channels[1] = get_limit_channel(node);
122 }
123
124 return limit_channels;
125}
126
127static int node_gpu_material(GPUMaterial *material,
128 bNode *node,
129 bNodeExecData * /*execdata*/,
132{
133 const float spill_channel = get_spill_channel(*node);
134 const float2 limit_channels = float2(get_limit_channels(*node));
135
136 return GPU_stack_link(material,
137 node,
138 "node_composite_color_spill",
139 inputs,
140 outputs,
141 GPU_constant(&spill_channel),
142 GPU_constant(limit_channels));
143}
144
145static float3 compute_spill_scale(const bool &use_spill_strength,
146 const float4 &spill_strength,
147 const int spill_channel)
148{
149 if (use_spill_strength) {
150 float3 scale = spill_strength.xyz();
151 scale[spill_channel] *= -1.0f;
152 return scale;
153 }
154
155 float3 scale = float3(0.0f);
156 scale[spill_channel] = -1.0f;
157 return scale;
158}
159
161{
162 const int spill_channel = get_spill_channel(builder.node());
163 const float2 limit_channels = float2(get_limit_channels(builder.node()));
164
166 return mf::build::SI5_SO<float4, float, float, bool, float4, float4>(
167 "Color Spill",
168 [=](const float4 &color,
169 const float &factor,
170 const float &limit_scale,
171 const bool &use_spill_strength,
172 const float4 &spill_strength) -> float4 {
173 float average_limit = (color[limit_channels.x] + color[limit_channels.y]) / 2.0f;
174 float map = factor * color[spill_channel] - limit_scale * average_limit;
175 float3 spill_scale = compute_spill_scale(
176 use_spill_strength, spill_strength, spill_channel);
177 return float4(map > 0.0f ? color.xyz() + spill_scale * map : color.xyz(), color.w);
178 },
179 mf::build::exec_presets::SomeSpanOrSingle<0>());
180 });
181}
182
183} // namespace blender::nodes::node_composite_color_spill_cc
184
186{
188
189 static blender::bke::bNodeType ntype;
190
191 cmp_node_type_base(&ntype, "CompositorNodeColorSpill", CMP_NODE_COLOR_SPILL);
192 ntype.ui_name = "Color Spill";
193 ntype.ui_description =
194 "Remove colors from a blue or green screen, by reducing one RGB channel compared to the "
195 "others";
196 ntype.enum_name_legacy = "COLOR_SPILL";
197 ntype.nclass = NODE_CLASS_MATTE;
198 ntype.declare = file_ns::cmp_node_color_spill_declare;
199 ntype.initfunc = file_ns::node_composit_init_color_spill;
202 ntype.gpu_fn = file_ns::node_gpu_material;
203 ntype.build_multi_function = file_ns::node_build_multi_function;
204
206}
#define NODE_CLASS_MATTE
Definition BKE_node.hh:440
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1215
#define CMP_NODE_COLOR_SPILL
#define IFACE_(msgid)
CMPNodeColorSpillLimitAlgorithm
@ CMP_NODE_COLOR_SPILL_LIMIT_ALGORITHM_AVERAGE
@ CMP_NODE_COLOR_SPILL_LIMIT_ALGORITHM_SINGLE
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
GPUNodeLink * GPU_constant(const float *num)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
@ PROP_FACTOR
Definition RNA_types.hh:239
@ UI_ITEM_R_SPLIT_EMPTY_NAME
@ UI_ITEM_R_EXPAND
DeclType::Builder & add_input(StringRef name, StringRef identifier="")
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
static int node_gpu_material(GPUMaterial *material, bNode *node, bNodeExecData *, GPUNodeStack *inputs, GPUNodeStack *outputs)
static void node_composit_init_color_spill(bNodeTree *, bNode *node)
static float3 compute_spill_scale(const bool &use_spill_strength, const float4 &spill_strength, const int spill_channel)
static void node_build_multi_function(blender::nodes::NodeMultiFunctionBuilder &builder)
static void cmp_node_color_spill_declare(NodeDeclarationBuilder &b)
static CMPNodeColorSpillLimitAlgorithm get_limit_algorithm(const bNode &node)
VecBase< float, 4 > float4
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
VecBase< float, 3 > float3
static void register_node_type_cmp_color_spill()
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
VecBase< T, 3 > xyz() const
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
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