Blender V5.3
node_composite_convert_color_space.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
6#include "UI_resources.hh"
7
9
10#include "GPU_shader.hh"
11
12#include "COM_node_operation.hh"
14#include "COM_utilities.hh"
15
17
19
21
23{
24 b.add_input<decl::Color>("Image"_ustr)
25 .default_value({1.0f, 1.0f, 1.0f, 1.0f})
26 .structure_type(StructureType::Dynamic);
27
28 b.add_output<decl::Color>("Image"_ustr).structure_type(StructureType::Dynamic);
29}
30
31static void node_init(bNodeTree * /*ntree*/, bNode *node)
32{
33 NodeConvertColorSpace *ncs = MEM_new<NodeConvertColorSpace>("node colorspace");
35 ncs->from_color_space, ncs->from_interop_id, "scene_linear");
37 node->storage = ncs;
38}
39
40static void node_draw_buttons(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
41{
42 layout.prop_with_menu(ptr,
43 "from_color_space",
45 std::nullopt,
46 ICON_NONE,
47 "UI_MT_color_space_select");
48 layout.prop_with_menu(ptr,
49 "to_color_space",
51 std::nullopt,
52 ICON_NONE,
53 "UI_MT_color_space_select");
54}
55
56using namespace blender::compositor;
57
59 public:
61
62 void execute() override
63 {
64 BLI_SCOPED_DEFER([&]() { this->populate_meta_data(); });
65
66 const Result &input_image = this->get_input("Image");
67 if (this->is_identity()) {
68 Result &output_image = this->get_result("Image");
69 output_image.share_data(input_image);
70 return;
71 }
72
73 if (input_image.is_single_value()) {
74 this->execute_single();
75 return;
76 }
77
78 if (this->context().use_gpu()) {
79 this->execute_gpu();
80 return;
81 }
82
83 this->execute_cpu();
84 }
85
87 {
88 const char *source = node_storage(node()).from_color_space;
89 const char *target = node_storage(node()).to_color_space;
90
93 context(), source, target);
94
96
97 /* A null shader indicates that the conversion shader is just a stub implementation since OCIO
98 * is disabled at compile time, so pass the input through in that case. */
99 const Result &input_image = this->get_input("Image");
100 Result &output_image = this->get_result("Image");
101 if (!shader) {
102 output_image.share_data(input_image);
103 return;
104 }
105
106 GPU_shader_uniform_1b(shader, "premultiply_output", false);
107
108 input_image.bind_as_texture(shader, ocio_shader.input_sampler_name());
109
110 const Domain domain = compute_domain();
111 output_image.allocate_texture(domain);
112 output_image.bind_as_image(shader, ocio_shader.output_image_name());
113
115
116 input_image.unbind_as_texture();
117 output_image.unbind_as_image();
118 ocio_shader.unbind_shader_and_resources();
119 }
120
122 {
123 const char *source = node_storage(node()).from_color_space;
124 const char *target = node_storage(node()).to_color_space;
126 target);
127
128 Result &input_image = get_input("Image");
129
130 const Domain domain = compute_domain();
131 Result &output_image = get_result("Image");
132 output_image.allocate_texture(domain);
133
134 parallel_for(domain.data_size, [&](const int2 texel) {
135 output_image.store_pixel(texel, input_image.load_pixel<Color>(texel));
136 });
137
138 color_processor.apply(static_cast<float *>(output_image.cpu_data_for_write().data()),
139 domain.data_size.x,
140 domain.data_size.y,
141 input_image.channels_count(),
142 false);
143 }
144
146 {
147 const char *source = node_storage(node()).from_color_space;
148 const char *target = node_storage(node()).to_color_space;
150 target);
151
152 Result &input_image = get_input("Image");
153 Color color = input_image.get_single_value<Color>();
154
155 color_processor.apply_pixel(color, 3);
156
157 Result &output_image = get_result("Image");
158 output_image.allocate_single_value();
159 output_image.set_single_value(color);
160 }
161
163 {
164 Result &output_image = this->get_result("Image");
165 if (IMB_colormanagement_space_name_is_data(node_storage(this->node()).to_color_space)) {
166 output_image.meta_data.is_non_color_data = true;
167 }
168 }
169
171 {
172 const char *source = node_storage(node()).from_color_space;
173 const char *target = node_storage(node()).to_color_space;
174
175 if (STREQ(source, target)) {
176 return true;
177 }
178
179 /* Data color spaces ignore any color transformation that gets applied to them. */
181 return true;
182 }
183
184 return false;
185 }
186};
187
189{
190 return new ConvertColorSpaceOperation(context, node);
191}
192
193static void node_register()
194{
195 static bke::bNodeType ntype;
196
197 cmp_node_type_base(&ntype, "CompositorNodeConvertColorSpace"_ustr, CMP_NODE_CONVERT_COLOR_SPACE);
198 ntype.ui_name = "Convert Colorspace";
199 ntype.ui_description = "Convert between color spaces";
200 ntype.enum_name_legacy = "CONVERT_COLORSPACE";
202 ntype.declare = node_declare;
205 ntype.initfunc = node_init;
207 ntype, "NodeConvertColorSpace", node_free_standard_storage, node_copy_standard_storage);
210
212}
214
215} // namespace blender::nodes::node_composite_convert_color_space_cc
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:484
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1353
#define CMP_NODE_CONVERT_COLOR_SPACE
#define BLI_SCOPED_DEFER(function_to_defer)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define STREQ(a, b)
void apply(float *buffer, int width, int height, int channels, bool predivide) const
static ColormanageProcessor colorspace_processor_new(StringRefNull from_colorspace, StringRefNull to_colorspace)
void apply_pixel(float *pixel, int channels) const
NodeOperation(Context &context, const bNode &node)
OCIOColorSpaceConversionShader & get(Context &context, std::string source, std::string target)
Result & get_result(StringRef identifier)
Definition operation.cc:40
Context & context() const
Definition operation.cc:57
Result & get_input(StringRef identifier) const
Definition operation.cc:35
virtual Domain compute_domain()
Definition operation.cc:62
void share_data(const Result &source)
Definition result.cc:996
void allocate_texture(const Domain domain, const bool from_pool=true, const std::optional< ResultStorageType > storage_type=std::nullopt)
Definition result.cc:554
void unbind_as_texture() const
Definition result.cc:964
int64_t channels_count() const
Definition result.cc:1180
void set_single_value(const T &value)
GMutableSpan cpu_data_for_write()
void bind_as_texture(gpu::Shader *shader, const char *texture_name) const
Definition result.cc:854
void unbind_as_image() const
Definition result.cc:990
void bind_as_image(gpu::Shader *shader, const char *image_name, bool read=false) const
Definition result.cc:951
const T & get_single_value() const
OCIOColorSpaceConversionShaderContainer ocio_color_space_conversion_shaders
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
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:6166
void compute_dispatch_threads_at_least(gpu::Shader *shader, int2 threads_range, int2 local_size=int2(16))
Definition utilities.cc:235
ColorSceneLinear4f< eAlpha::Premultiplied > Color
Definition COM_result.hh:96
void parallel_for(const int2 range, const Function &function)
static NodeOperation * get_compositor_operation(Context &context, const bNode &node)
static void node_draw_buttons(ui::Layout &layout, bContext *, PointerRNA *ptr)
void GPU_shader_uniform_1b(gpu::Shader *sh, const char *name, bool value)
void IMB_colormanagement_colorspace_name_set(char *name, char *interop_id, const char *new_name)
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:57
void node_free_standard_storage(bNode *node)
Definition node_util.cc:45
bool IMB_colormanagement_space_name_is_data(const char *name)
void cmp_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
static constexpr int _160
Definition BKE_node.hh:246
Defines a node type.
Definition BKE_node.hh:265
void(*) draw_buttons(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:290
std::string ui_description
Definition BKE_node.hh:271
NodeGetCompositorOperationFunction get_compositor_operation
Definition BKE_node.hh:379
void(*) initfunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:320
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393
void prop_with_menu(PointerRNA *ptr, blender::StringRefNull propname, eUI_Item_Flag flag, std::optional< blender::StringRefNull > name, int icon, const char *menu_type)