Blender V5.3
node_composite_alpha_convert.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
6
8
10
11#include "GPU_material.hh"
12
13#include "COM_result.hh"
14
16
18
19static const EnumPropertyItem type_items[] = {
21 "STRAIGHT_TO_PREMULTIPLIED",
22 0,
23 N_("To Premultiplied"),
24 N_("Convert straight to premultiplied")},
26 "PREMULTIPLIED_TO_STRAIGHT",
27 0,
28 N_("To Straight"),
29 N_("Convert premultiplied to straight")},
30 {0, nullptr, 0, nullptr, nullptr},
31};
32
34{
35 b.use_custom_socket_order();
36 b.allow_any_socket_order();
37 b.is_function_node();
38 b.add_input<decl::Color>("Image"_ustr).default_value({1.0f, 1.0f, 1.0f, 1.0f}).hide_value();
39 b.add_output<decl::Color>("Image"_ustr).align_with_previous();
40
41 b.add_input<decl::Menu>("Type"_ustr)
43 .static_items(type_items)
45}
46
47using namespace blender::compositor;
48
49static int node_gpu_material(GPUMaterial *material,
50 bNode *node,
51 bNodeExecData * /*execdata*/,
52 GPUNodeStack *inputs,
53 GPUNodeStack *outputs)
54{
55 return GPU_stack_link(material, node, "node_composite_convert_alpha", inputs, outputs);
56}
57
58static float4 convert_alpha(const float4 &color, const MenuValue &type)
59{
60 switch (CMPNodeAlphaConvertMode(type.value)) {
62 return float4(color.xyz() * color.w, color.w);
64 return color.w == 0.0f ? color : float4(color.xyz() / color.w, color.w);
65 }
66 return color;
67}
68
70
72{
73 static auto function = mf::build::SI2_SO<Color, MenuValue, Color>(
74 "Alpha Convert",
75 [](const Color &color, const MenuValue &type) -> Color {
76 return Color(convert_alpha(float4(color), type));
77 },
78 mf::build::exec_presets::AllSpanOrSingle());
79 builder.set_matching_fn(function);
80}
81
82static void node_register()
83{
84 static bke::bNodeType ntype;
85
86 cmp_node_type_base(&ntype, "CompositorNodePremulKey"_ustr, CMP_NODE_PREMULKEY);
87 ntype.ui_name = "Alpha Convert";
88 ntype.ui_description = "Convert to and from premultiplied (associated) alpha";
89 ntype.enum_name_legacy = "PREMULKEY";
91 ntype.declare = node_declare;
94
96}
98
99} // namespace blender::nodes::node_composite_alpha_convert_cc
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:484
#define CMP_NODE_PREMULKEY
#define NOD_REGISTER_NODE(REGISTER_FUNC)
void set_matching_fn(const mf::MultiFunction *fn)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
ColorSceneLinear4f< eAlpha::Premultiplied > Color
Definition COM_result.hh:96
static float4 convert_alpha(const float4 &color, const MenuValue &type)
static void node_build_multi_function(nodes::NodeMultiFunctionBuilder &builder)
static int node_gpu_material(GPUMaterial *material, bNode *node, bNodeExecData *, GPUNodeStack *inputs, GPUNodeStack *outputs)
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
@ CMP_NODE_ALPHA_CONVERT_PREMULTIPLY
@ CMP_NODE_ALPHA_CONVERT_UNPREMULTIPLY
void cmp_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
Defines a node type.
Definition BKE_node.hh:265
std::string ui_description
Definition BKE_node.hh:271
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:373
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:382
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393
#define N_(msgid)