Blender V5.3
node_composite_alpha_over.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
5#include "BLI_math_vector.hh"
7
9
10#include "NOD_multi_function.hh"
11
12#include "UI_resources.hh"
13
14#include "GPU_material.hh"
15
16#include "COM_result.hh"
17
19
21
22static const EnumPropertyItem type_items[] = {
24 "OVER",
25 ICON_NONE,
26 N_("Over"),
27 N_("The foreground goes over the background according to the alpha of the foreground")},
29 "DISJOINT_OVER",
30 ICON_NONE,
31 N_("Disjoint Over"),
32 N_("The foreground goes over the background according to the alpha of the foreground while "
33 "assuming the background is being held out by the foreground")},
35 "CONJOINT_OVER",
36 ICON_NONE,
37 N_("Conjoint Over"),
38 N_("The foreground goes over the background according to the alpha of the foreground but the "
39 "foreground completely covers the background if it is more opaque")},
40 {0, nullptr, 0, nullptr, nullptr},
41};
42
44{
45 b.is_function_node();
46 b.add_input<decl::Color>("Background"_ustr).default_value({1.0f, 1.0f, 1.0f, 1.0f});
47 b.add_input<decl::Color>("Foreground"_ustr).default_value({1.0f, 1.0f, 1.0f, 1.0f});
48 b.add_input<decl::Float>("Factor"_ustr, "Fac"_ustr)
49 .default_value(1.0f)
50 .min(0.0f)
51 .max(1.0f)
53 b.add_input<decl::Menu>("Type"_ustr)
55 .static_items(type_items)
57 b.add_input<decl::Bool>("Straight Alpha"_ustr)
58 .default_value(false)
60 "Defines whether the foreground is in straight alpha form, which is necessary to know "
61 "for proper alpha compositing. Images in the compositor are in premultiplied alpha form "
62 "by default, so this should be false in most cases. But if, and only if, the foreground "
63 "was converted to straight alpha form for some reason, this should be set to true");
64 b.add_output<decl::Color>("Image"_ustr);
65}
66
67static int node_gpu_material(GPUMaterial *material,
68 bNode *node,
69 bNodeExecData * /*execdata*/,
70 GPUNodeStack *inputs,
71 GPUNodeStack *outputs)
72{
73 return GPU_stack_link(material, node, "node_composite_alpha_over", inputs, outputs);
74}
75
76/* If straight_alpha is true, then the foreground is in straight alpha form and would need to be
77 * premultiplied. */
78static float4 preprocess_foreground(const float4 &foreground, const bool straight_alpha)
79{
80 const float alpha = math::clamp(foreground.w, 0.0f, 1.0f);
81 const float4 premultiplied_foreground = float4(foreground.xyz() * alpha, alpha);
82 return straight_alpha ? premultiplied_foreground : foreground;
83}
84
85/* Computes the Porter and Duff Over compositing operation. */
86static float4 alpha_over(const float4 &background,
87 const float4 &foreground,
88 const float factor,
89 const bool straight_alpha)
90{
91 const float4 foreground_color = preprocess_foreground(foreground, straight_alpha);
92
93 const float foreground_alpha = math::clamp(foreground.w, 0.0f, 1.0f);
94 const float4 mix_result = foreground_color + background * (1.0f - foreground_alpha);
95
96 return math::interpolate(background, mix_result, factor);
97}
98
99/* Computes the Porter and Duff Over compositing operation while assuming the background is being
100 * held out by the foreground. See for reference:
101 *
102 * https://benmcewan.com/blog/disjoint-over-and-conjoint-over-explained */
103static float4 alpha_over_disjoint(const float4 &background,
104 const float4 &foreground,
105 const float factor,
106 const bool straight_alpha)
107{
108 const float4 foreground_color = preprocess_foreground(foreground, straight_alpha);
109
110 const float foreground_alpha = math::clamp(foreground.w, 0.0f, 1.0f);
111 const float background_alpha = math::clamp(background.w, 0.0f, 1.0f);
112
113 if (foreground_alpha + background_alpha < 1.0f) {
114 const float4 mix_result = foreground_color + background;
115 return math::interpolate(background, mix_result, factor);
116 }
117
118 const float4 straight_background = math::safe_divide(background, background_alpha);
119 const float4 mix_result = foreground_color + straight_background * (1.0f - foreground_alpha);
120
121 return math::interpolate(background, mix_result, factor);
122}
123
124/* Computes the Porter and Duff Over compositing operation but the foreground completely covers the
125 * background if it is more opaque but not necessary completely opaque. See for reference:
126 *
127 * https://benmcewan.com/blog/disjoint-over-and-conjoint-over-explained */
128static float4 alpha_over_conjoint(const float4 &background,
129 const float4 &foreground,
130 const float factor,
131 const bool straight_alpha)
132{
133 const float4 foreground_color = preprocess_foreground(foreground, straight_alpha);
134
135 const float foreground_alpha = math::clamp(foreground.w, 0.0f, 1.0f);
136 const float background_alpha = math::clamp(background.w, 0.0f, 1.0f);
137
138 if (foreground_alpha > background_alpha) {
139 const float4 mix_result = foreground_color;
140 return math::interpolate(background, mix_result, factor);
141 }
142
143 const float alpha_ratio = math::safe_divide(foreground_alpha, background_alpha);
144 const float4 mix_result = foreground_color + background * (1.0f - alpha_ratio);
145
146 return math::interpolate(background, mix_result, factor);
147}
148
150
152{
153 static auto function = mf::build::SI5_SO<Color, Color, float, MenuValue, bool, Color>(
154 "Alpha Over",
155 [=](const Color &background,
156 const Color &foreground,
157 const float factor,
158 const MenuValue type,
159 const bool straight_alpha) -> Color {
160 switch (CMPNodeAlphaOverOperationType(type.value)) {
162 return Color(
163 alpha_over(float4(background), float4(foreground), factor, straight_alpha));
166 float4(background), float4(foreground), factor, straight_alpha));
169 float4(background), float4(foreground), factor, straight_alpha));
170 }
171 return background;
172 },
173 mf::build::exec_presets::SomeSpanOrSingle<0, 1>());
174
175 builder.set_matching_fn(function);
176}
177
178static void node_register()
179{
180 static bke::bNodeType ntype;
181
182 cmp_node_type_base(&ntype, "CompositorNodeAlphaOver"_ustr, CMP_NODE_ALPHAOVER);
183 ntype.ui_name = "Alpha Over";
184 ntype.ui_description = "Overlay a foreground image onto a background image";
185 ntype.enum_name_legacy = "ALPHAOVER";
187 ntype.declare = node_declare;
190
192}
194
195} // namespace blender::nodes::node_composite_alpha_over_cc
#define NODE_CLASS_OP_COLOR
Definition BKE_node.hh:480
#define CMP_NODE_ALPHAOVER
#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
T clamp(const T &a, const T &min, const T &max)
T safe_divide(const T &a, const T &b)
T interpolate(const T &a, const T &b, const FactorT &t)
static float4 preprocess_foreground(const float4 &foreground, const bool straight_alpha)
static float4 alpha_over_conjoint(const float4 &background, const float4 &foreground, const float factor, const bool straight_alpha)
static int node_gpu_material(GPUMaterial *material, bNode *node, bNodeExecData *, GPUNodeStack *inputs, GPUNodeStack *outputs)
static float4 alpha_over(const float4 &background, const float4 &foreground, const float factor, const bool straight_alpha)
static void node_declare(NodeDeclarationBuilder &b)
static float4 alpha_over_disjoint(const float4 &background, const float4 &foreground, const float factor, const bool straight_alpha)
static void node_build_multi_function(nodes::NodeMultiFunctionBuilder &builder)
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
VecBase< float, 4 > float4
CMPNodeAlphaOverOperationType
@ CMP_NODE_ALPHA_OVER_OPERATION_TYPE_CONJOINT_OVER
@ CMP_NODE_ALPHA_OVER_OPERATION_TYPE_DISJOINT_OVER
@ CMP_NODE_ALPHA_OVER_OPERATION_TYPE_OVER
@ PROP_FACTOR
Definition RNA_types.hh:308
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)