26 b.use_custom_socket_order();
27 b.allow_any_socket_order();
30 .default_value({0.8f, 0.8f, 0.8f, 1.0f})
34 .structure_type(StructureType::Dynamic)
35 .align_with_previous();
37 b.add_output<
decl::Float>(
"Matte"_ustr).structure_type(StructureType::Dynamic);
39 .structure_type(StructureType::Dynamic)
43 .default_value({1.0f, 1.0f, 1.0f, 1.0f})
44 .structure_type(StructureType::Dynamic);
52 "Blur the color of the input image in YCC color space before keying while leaving the "
53 "luminance intact using a Gaussian blur of the given size");
64 "Balances between the two non primary color channels that the primary channel compares "
65 "against. 0 means the latter channel of the two is used, while 1 means the former of "
75 "The matte gets remapped such matte values lower than the black level become black. "
76 "Pixels at the identified edges are excluded from the remapping to preserve details");
83 "The matte gets remapped such matte values higher than the white level become white. "
84 "Pixels at the identified edges are excluded from the remapping to preserve details");
94 "Size of the search window used to identify edges. Higher search size corresponds to "
95 "less noisy and higher quality edges, not necessarily bigger edges. Edge tolerance can "
96 "be used to expend the size of the edges");
103 "Pixels are considered part of the edges if more than 10% of the neighbouring pixels "
104 "have matte values that differ from the pixel's matte value by this tolerance");
112 .structure_type(StructureType::Dynamic)
113 .description(
"Areas in the garbage matte mask are excluded from the matte");
119 .structure_type(StructureType::Dynamic)
120 .description(
"Areas in the core matte mask are included in the matte");
123 b.add_panel(
"Postprocess"_ustr).default_closed(
true);
124 postprocess_panel.
add_input<
decl::Int>(
"Blur Size"_ustr,
"Postprocess Blur Size"_ustr)
128 .description(
"Blur the computed matte using a Gaussian blur of the given size");
129 postprocess_panel.
add_input<
decl::Int>(
"Dilate Size"_ustr,
"Postprocess Dilate Size"_ustr)
133 "Dilate or erode the computed matte using a circular structuring element of the "
134 "specified size. Negative sizes means erosion while positive means dilation");
135 postprocess_panel.
add_input<
decl::Int>(
"Feather Size"_ustr,
"Postprocess Feather Size"_ustr)
139 "Dilate or erode the computed matte using an inverse distance operation evaluated at "
140 "the given falloff of the specified size. Negative sizes means erosion while positive "
154 .description(
"Specifies the strength of the despill");
161 "Defines the channel used for despill limiting. Balances between the two non primary "
162 "color channels that the primary channel compares against. 0 means the latter channel "
163 "of the two is used, while 1 means the former of the two is used");
232 if (blur_size == 0.0f) {
248 return blurred_input;
253 return math::max(0, this->
get_input(
"Preprocess Blur Size").get_single_value_default<int>());
258 if (this->
context().use_gpu()) {
293 const Color color = input.load_pixel<Color>(texel);
302 color_ycca /= 255.0f;
303 color_ycca.w = color.a;
305 output.store_pixel(texel, Color(color_ycca));
313 if (this->
context().use_gpu()) {
351 const Color color = input.load_pixel<Color>(texel);
361 const float2 new_chroma_cb_cr = float4(new_chroma.load_pixel<Color>(texel)).yz();
362 color_ycca.y = new_chroma_cb_cr.x * 255.0f;
363 color_ycca.z = new_chroma_cb_cr.y * 255.0f;
366 ycc_to_rgb(color_ycca.x,
373 color_rgba.w = color.a;
375 output.store_pixel(texel, Color(color_rgba));
383 if (this->
context().use_gpu()) {
424 auto compute_saturation_indices = [](
const float3 &
v) {
425 int index_of_max = ((
v.x >
v.y) ? ((
v.x >
v.z) ? 0 : 2) : ((
v.y >
v.z) ? 1 : 2));
426 int2 other_indices = (
int2(index_of_max) +
int2(1, 2)) % 3;
427 int min_index =
math::min(other_indices.x, other_indices.y);
428 int max_index =
math::max(other_indices.x, other_indices.y);
429 return int3(index_of_max, max_index, min_index);
432 auto compute_saturation = [&](
const float4 &
color,
const int3 &indices) {
434 return (
color[indices.x] - weighted_average) *
math::abs(1.0f - weighted_average);
438 float4 input_color = float4(input.load_pixel<Color>(texel));
442 if (math::reduce_min(input_color) > 1.0f) {
443 output.store_pixel(texel, 1.0f);
448 int3 key_saturation_indices = compute_saturation_indices(key_color.xyz());
449 float input_saturation = compute_saturation(input_color, key_saturation_indices);
450 float key_saturation = compute_saturation(key_color, key_saturation_indices);
453 if (input_saturation < 0.0f) {
458 else if (input_saturation >= key_saturation) {
464 matte = 1.0f -
math::clamp(input_saturation / key_saturation, 0.0f, 1.0f);
467 output.store_pixel(texel, matte);
476 this->
get_input(
"Key Balance").get_single_value_default<float>(), 0.0f, 1.0f);
490 if (!output_edges.
should_compute() && black_level == 0.0f && white_level == 1.0f &&
499 if (this->
context().use_gpu()) {
519 shader,
"garbage_matte_tx");
552 return output_edges.
should_compute() ?
"compositor_keying_tweak_matte_with_edges" :
553 "compositor_keying_tweak_matte_without_edges";
576 float matte = input_matte.load_pixel<float>(texel);
581 bool is_edge = false;
582 if (compute_edges || black_level != 0.0f || white_level != 1.0f) {
586 for (int j = -edge_search_radius; j <= edge_search_radius; j++) {
587 for (int i = -edge_search_radius; i <= edge_search_radius; i++) {
588 float neighbor_matte = input_matte.load_pixel_extended<float>(texel + int2(i, j));
589 count += int(math::distance(matte, neighbor_matte) < edge_tolerance);
596 is_edge = count < ((edge_search_radius * 2 + 1) * (edge_search_radius * 2 + 1)) * 0.9f;
599 float tweaked_matte = matte;
604 if (!is_edge && white_level != black_level) {
606 (matte - black_level) / (white_level - black_level), 0.0f, 1.0f);
611 float garbage_matte = garbage_matte_image.
load_pixel<
float,
true>(texel);
612 tweaked_matte =
math::min(tweaked_matte, 1.0f - garbage_matte);
616 tweaked_matte =
math::max(tweaked_matte, core_matte);
620 output_edges.
store_pixel(texel, is_edge ? 1.0f : 0.0f);
629 return math::max(0, this->
get_input(
"Edge Search Size").get_single_value_default<int>());
635 this->
get_input(
"Edge Tolerance").get_single_value_default<float>(), 0.0f, 1.0f);
641 this->
get_input(
"Black Level").get_single_value_default<float>(), 0.0f, 1.0f);
647 this->
get_input(
"White Level").get_single_value_default<float>(), 0.0f, 1.0f);
656 if (blur_size == 0.0f) {
666 return blurred_matte;
671 return math::max(0, this->
get_input(
"Postprocess Blur Size").get_single_value_default<int>());
689 return morphed_matte;
694 return this->
get_input(
"Postprocess Dilate Size").get_single_value_default<
int>();
713 return feathered_matte;
718 return this->
get_input(
"Postprocess Feather Size").get_single_value_default<
int>();
723 return this->
get_input(
"Feather Falloff").get_single_value_default<
MenuValue>().value;
728 if (this->
context().use_gpu()) {
776 auto compute_saturation_indices = [](
const float3 &
v) {
777 int index_of_max = ((
v.x >
v.y) ? ((
v.x >
v.z) ? 0 : 2) : ((
v.y >
v.z) ? 1 : 2));
778 int2 other_indices = (
int2(index_of_max) +
int2(1, 2)) % 3;
779 int min_index =
math::min(other_indices.x, other_indices.y);
780 int max_index =
math::max(other_indices.x, other_indices.y);
781 return int3(index_of_max, max_index, min_index);
785 float4 key_color = float4(key.load_pixel<Color, true>(texel));
786 float4 color = float4(input.load_pixel<Color>(texel));
787 float matte = matte_image.load_pixel<float>(texel);
793 int3 indices = compute_saturation_indices(key_color.xyz());
794 float weighted_average = math::interpolate(
795 color[indices.y], color[indices.z], despill_balance);
796 color[indices.x] -= math::max(0.0f, (color[indices.x] - weighted_average) * despill_factor);
798 output.store_pixel(texel, Color(color));
804 return math::max(0.0f, this->
get_input(
"Despill Strength").get_single_value_default<float>());
810 this->
get_input(
"Despill Balance").get_single_value_default<float>(), 0.0f, 1.0f);
826 "Perform both chroma keying (to remove the backdrop) and despill (to correct color cast "
827 "from the backdrop)";
#define BLT_I18NCONTEXT_ID_NODETREE
#define BLT_I18NCONTEXT_ID_IMAGE
#define BLT_I18NCONTEXT_ID_CURVE_LEGACY
#define NOD_REGISTER_NODE(REGISTER_FUNC)
Result create_result(ResultType type, ResultPrecision precision)
gpu::Shader * get_shader(const char *info_name, ResultPrecision precision)
NodeOperation(Context &context, const bNode &node)
Result & get_result(StringRef identifier)
void allocate_default_remaining_outputs()
Context & context() const
Result & get_input(StringRef identifier) const
void share_data(const Result &source)
bool is_single_value() const
void unbind_as_texture_or_single_value(gpu::Texture *single_value_texture) const
void store_pixel(const int2 &texel, const T &pixel_value)
bool should_compute() const
void allocate_texture(const Domain domain, const bool from_pool=true, const std::optional< ResultStorageType > storage_type=std::nullopt)
void unbind_as_texture() const
void bind_as_texture(gpu::Shader *shader, const char *texture_name) const
T get_single_value_default() const
T load_pixel(const int2 &texel) const
void unbind_as_image() const
void bind_as_image(gpu::Shader *shader, const char *image_name, bool read=false) const
gpu::Texture * bind_as_texture_or_single_value(gpu::Shader *shader, const char *texture_name) const
DeclType::Builder & add_input(UString name, UString identifier=""_ustr)
PanelDeclarationBuilder & add_panel(UString name, int identifier=-1)
Self & default_closed(bool closed)
Self & translation_context(std::optional< std::string > value=std::nullopt)
StructureType structure_type
std::optional< std::string > translation_context
NodeOperation(Context &context, const bNode &node)
int get_postprocess_blur_size()
Result extract_input_chroma()
float get_edge_tolerance()
int get_preprocess_blur_size()
Result extract_input_chroma_gpu()
float get_despill_balance()
int get_edge_search_size()
Result compute_tweaked_matte_gpu(Result &input_matte)
Result compute_feathered_matte(Result &input_matte)
int get_postprocess_feather_size()
Result compute_tweaked_matte_cpu(Result &input_matte)
Result replace_input_chroma_gpu(Result &new_chroma)
Result compute_blurred_input()
Result compute_morphed_matte(Result &input_matte)
void compute_image_gpu(Result &matte)
Result compute_matte(Result &input)
Result compute_tweaked_matte(Result &input_matte)
Result compute_matte_cpu(Result &input)
Result compute_blurred_matte(Result &input_matte)
Result extract_input_chroma_cpu()
Result compute_matte_gpu(Result &input)
Result replace_input_chroma(Result &new_chroma)
int get_feather_falloff()
const char * get_tweak_matte_shader_name()
Result replace_input_chroma_cpu(Result &new_chroma)
int get_postprocess_dilate_size()
void compute_image(Result &matte)
float get_despill_strength()
void compute_image_cpu(Result &matte_image)
float distance(float, float) RET
void node_register_type(bNodeType &ntype)
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))
void compute_dispatch_threads_at_least(gpu::Shader *shader, int2 threads_range, int2 local_size=int2(16))
void symmetric_separable_blur(Context &context, const Result &input, Result &output, const float2 &radius, const math::FilterKernel filter_type=math::FilterKernel::Gauss, const bool extend_bounds=false)
ColorSceneLinear4f< eAlpha::Premultiplied > Color
void morphological_distance(Context &context, const Result &input, Result &output, const int distance)
void morphological_distance_feather(Context &context, const Result &input, Result &output, const int distance, const int falloff_type=PROP_SMOOTH)
void parallel_for(const int2 range, const Function &function)
T clamp(const T &a, const T &min, const T &max)
T min(const T &a, const T &b)
T interpolate(const T &a, const T &b, const FactorT &t)
T max(const T &a, const T &b)
static NodeOperation * get_compositor_operation(Context &context, const bNode &node)
static void node_register()
static void node_init(bNodeTree *, bNode *node)
static void node_declare(NodeDeclarationBuilder &b)
ATTR_WARN_UNUSED_RESULT const BMVert * v
BMesh const char void * data
const EnumPropertyItem rna_enum_proportional_falloff_curve_only_items[]
void GPU_shader_bind(gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants_state=nullptr)
void GPU_shader_uniform_1i(gpu::Shader *sh, const char *name, int value)
void GPU_shader_uniform_1f(gpu::Shader *sh, const char *name, float value)
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
void node_free_standard_storage(bNode *node)
void cmp_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
static constexpr int _160
std::string ui_description
NodeGetCompositorOperationFunction get_compositor_operation
void(*) initfunc(bNodeTree *ntree, bNode *node)
const char * enum_name_legacy
NodeDeclareFunction declare