Blender V5.3
node_composite_tone_map.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 <cmath>
6
7#include "BLI_math_base.hh"
8#include "BLI_math_vector.hh"
10
11#include "RNA_types.hh"
12
14
16#include "COM_node_operation.hh"
17#include "COM_utilities.hh"
18
20
22
23static const EnumPropertyItem type_items[] = {
25 "RD_PHOTORECEPTOR",
26 0,
27 N_("R/D Photoreceptor"),
28 N_("More advanced algorithm based on eye physiology, by Reinhard and Devlin")},
30 "RH_SIMPLE",
31 0,
32 N_("Rh Simple"),
33 N_("Simpler photographic algorithm by Reinhard")},
34 {0, nullptr, 0, nullptr, nullptr},
35};
36
38{
39 b.use_custom_socket_order();
40 b.allow_any_socket_order();
41
42 b.add_input<decl::Color>("Image"_ustr)
43 .default_value({1.0f, 1.0f, 1.0f, 1.0f})
44 .hide_value()
45 .structure_type(StructureType::Dynamic);
46
47 b.add_output<decl::Color>("Image"_ustr)
48 .structure_type(StructureType::Dynamic)
49 .align_with_previous();
50
51 b.add_input<decl::Menu>("Type"_ustr)
53 .static_items(type_items)
55
56 b.add_input<decl::Float>("Key"_ustr)
57 .default_value(0.18f)
58 .min(0.0f)
59 .usage_by_single_menu(CMP_NODE_TONE_MAP_SIMPLE)
61 "The luminance that will be mapped to the log average luminance, typically set to the "
62 "middle gray value");
63 b.add_input<decl::Float>("Balance"_ustr)
64 .default_value(1.0f)
65 .min(0.0f)
66 .usage_by_single_menu(CMP_NODE_TONE_MAP_SIMPLE)
68 "Balances low and high luminance areas. Lower values emphasize details in shadows, "
69 "while higher values compress highlights more smoothly");
70 b.add_input<decl::Float>("Gamma"_ustr)
71 .default_value(1.0f)
72 .min(0.0f)
73 .usage_by_single_menu(CMP_NODE_TONE_MAP_SIMPLE)
74 .description("Gamma correction factor applied after tone mapping");
75
76 b.add_input<decl::Float>("Intensity"_ustr)
77 .default_value(0.0f)
78 .usage_by_single_menu(CMP_NODE_TONE_MAP_PHOTORECEPTOR)
80 "Controls the intensity of the image, lower values makes it darker while higher values "
81 "makes it lighter");
82 b.add_input<decl::Float>("Contrast"_ustr)
83 .default_value(0.0f)
84 .min(0.0f)
85 .usage_by_single_menu(CMP_NODE_TONE_MAP_PHOTORECEPTOR)
87 "Controls the contrast of the image. Zero automatically sets the contrast based on its "
88 "global range for better luminance distribution");
89 b.add_input<decl::Float>("Light Adaptation"_ustr)
90 .default_value(0.0f)
92 .min(0.0f)
93 .max(1.0f)
94 .usage_by_single_menu(CMP_NODE_TONE_MAP_PHOTORECEPTOR)
95 .description(
96 "Specifies if tone mapping operates on the entire image or per pixel, 0 means the "
97 "entire image, 1 means it is per pixel, and values in between blends between both");
98 b.add_input<decl::Float>("Chromatic Adaptation"_ustr)
99 .default_value(0.0f)
101 .min(0.0f)
102 .max(1.0f)
103 .usage_by_single_menu(CMP_NODE_TONE_MAP_PHOTORECEPTOR)
104 .description(
105 "Specifies if tone mapping operates on the luminance or on each channel independently, "
106 "0 means it uses luminance, 1 means it is per channel, and values in between blends "
107 "between both");
108}
109
110static void node_init(bNodeTree * /*ntree*/, bNode *node)
111{
112 /* Unused, but still allocated for forward compatibility. */
113 NodeTonemap *ntm = MEM_new<NodeTonemap>(__func__);
114 node->storage = ntm;
115}
116
117using namespace blender::compositor;
118
120 public:
122
123 void execute() override
124 {
125 const Result &input_image = this->get_input("Image");
126 Result &output_image = this->get_result("Image");
127 if (input_image.is_single_value()) {
128 output_image.share_data(input_image);
129 return;
130 }
131
132 switch (get_type()) {
135 return;
138 return;
139 }
140
141 output_image.share_data(input_image);
142 }
143
144 /* Tone mapping based on equation (3) from Reinhard, Erik, et al. "Photographic tone reproduction
145 * for digital images." Proceedings of the 29th annual conference on Computer graphics and
146 * interactive techniques. 2002. */
148 {
149 if (this->context().use_gpu()) {
151 }
152 else {
154 }
155 }
156
158 {
159 const float luminance_scale = compute_luminance_scale();
160 const float luminance_scale_blend_factor = compute_luminance_scale_blend_factor();
161 const float gamma = this->get_gamma();
162 const float inverse_gamma = gamma != 0.0f ? 1.0f / gamma : 0.0f;
163
164 gpu::Shader *shader = context().get_shader("compositor_tone_map_simple");
166
167 GPU_shader_uniform_1f(shader, "luminance_scale", luminance_scale);
168 GPU_shader_uniform_1f(shader, "luminance_scale_blend_factor", luminance_scale_blend_factor);
169 GPU_shader_uniform_1f(shader, "inverse_gamma", inverse_gamma);
170
171 const Result &input_image = get_input("Image");
172 input_image.bind_as_texture(shader, "input_tx");
173
174 const Domain domain = compute_domain();
175 Result &output_image = get_result("Image");
176 output_image.allocate_texture(domain);
177 output_image.bind_as_image(shader, "output_img");
178
180
182 output_image.unbind_as_image();
183 input_image.unbind_as_texture();
184 }
185
187 {
188 const float luminance_scale = compute_luminance_scale();
189 const float luminance_scale_blend_factor = compute_luminance_scale_blend_factor();
190 const float gamma = this->get_gamma();
191 const float inverse_gamma = gamma != 0.0f ? 1.0f / gamma : 0.0f;
192
193 const Result &image = get_input("Image");
194
195 const Domain domain = compute_domain();
196 Result &output = get_result("Image");
197 output.allocate_texture(domain);
198
199 parallel_for(domain.data_size, [&](const int2 texel) {
200 float4 input_color = float4(image.load_pixel<Color>(texel));
201
202 /* Equation (2) from Reinhard's 2002 paper. */
203 float4 scaled_color = input_color * luminance_scale;
204
205 /* Equation (3) from Reinhard's 2002 paper, but with the 1 replaced with the blend factor for
206 * more flexibility. See ToneMapOperation::compute_luminance_scale_blend_factor. */
207 float4 denominator = luminance_scale_blend_factor + scaled_color;
208 float4 tone_mapped_color = math::safe_divide(scaled_color, denominator);
209
210 if (inverse_gamma != 0.0f) {
211 tone_mapped_color = math::pow(math::max(tone_mapped_color, float4(0.0f)), inverse_gamma);
212 }
213
214 output.store_pixel(texel, Color(float4(tone_mapped_color.xyz(), input_color.w)));
215 });
216 }
217
218 /* Computes the scaling factor in equation (2) from Reinhard's 2002 paper. */
220 {
221 const float geometric_mean = compute_geometric_mean_of_luminance();
222 return geometric_mean != 0.0 ? this->get_key() / geometric_mean : 0.0f;
223 }
224
225 /* Computes equation (1) from Reinhard's 2002 paper. However, note that the equation in the paper
226 * is most likely wrong, and the intention is actually to compute the geometric mean through a
227 * logscale arithmetic mean, that is, the division should happen inside the exponential function,
228 * not outside of it. That's because the sum of the log luminance will be a very large negative
229 * number, whose exponential will almost always be zero, which is unexpected and useless. */
231 {
232 return std::exp(compute_average_log_luminance());
233 }
234
235 float get_key()
236 {
237 return math::max(0.0f, this->get_input("Key").get_single_value_default<float>());
238 }
239
240 /* Equation (3) from Reinhard's 2002 paper blends between high luminance scaling for high
241 * luminance values and low luminance scaling for low luminance values. This is done by adding 1
242 * to the denominator, since for low luminance values, the denominator will be close to 1 and for
243 * high luminance values, the 1 in the denominator will be relatively insignificant. But the
244 * response of such function is not always ideal, so in this implementation, the 1 was exposed as
245 * a parameter to the user for more flexibility. */
247 {
248 return math::max(0.0f, this->get_input("Balance").get_single_value_default<float>());
249 }
250
251 float get_gamma()
252 {
253 return math::max(0.0f, this->get_input("Gamma").get_single_value_default<float>());
254 }
255
256 /* Tone mapping based on equation (1) and the trilinear interpolation between equations (6) and
257 * (7) from Reinhard, Erik, and Kate Devlin. "Dynamic range reduction inspired by photoreceptor
258 * physiology." IEEE transactions on visualization and computer graphics 11.1 (2005): 13-24. */
260 {
261 if (this->context().use_gpu()) {
263 }
264 else {
266 }
267 }
268
270 {
271 const float4 global_adaptation_level = compute_global_adaptation_level();
272 const float contrast = compute_contrast();
273 const float intensity = compute_intensity();
274 const float chromatic_adaptation = get_chromatic_adaptation();
275 const float light_adaptation = get_light_adaptation();
276
277 gpu::Shader *shader = context().get_shader("compositor_tone_map_photoreceptor");
279
280 GPU_shader_uniform_4fv(shader, "global_adaptation_level", global_adaptation_level);
281 GPU_shader_uniform_1f(shader, "contrast", contrast);
282 GPU_shader_uniform_1f(shader, "intensity", intensity);
283 GPU_shader_uniform_1f(shader, "chromatic_adaptation", chromatic_adaptation);
284 GPU_shader_uniform_1f(shader, "light_adaptation", light_adaptation);
285
286 float luminance_coefficients[3];
288 GPU_shader_uniform_3fv(shader, "luminance_coefficients", luminance_coefficients);
289
290 const Result &input_image = get_input("Image");
291 input_image.bind_as_texture(shader, "input_tx");
292
293 const Domain domain = compute_domain();
294 Result &output_image = get_result("Image");
295 output_image.allocate_texture(domain);
296 output_image.bind_as_image(shader, "output_img");
297
299
301 output_image.unbind_as_image();
302 input_image.unbind_as_texture();
303 }
304
306 {
307 const float4 global_adaptation_level = compute_global_adaptation_level();
308 const float contrast = compute_contrast();
309 const float intensity = compute_intensity();
310 const float chromatic_adaptation = get_chromatic_adaptation();
311 const float light_adaptation = get_light_adaptation();
312
313 float3 luminance_coefficients;
315
316 const Result &input = get_input("Image");
317
318 const Domain domain = compute_domain();
319 Result &output = get_result("Image");
320 output.allocate_texture(domain);
321
322 parallel_for(domain.data_size, [&](const int2 texel) {
323 float4 input_color = float4(input.load_pixel<Color>(texel));
324 float input_luminance = math::dot(input_color.xyz(), luminance_coefficients);
325
326 /* Trilinear interpolation between equations (6) and (7) from Reinhard's 2005 paper. */
327 float4 local_adaptation_level = math::interpolate(
328 float4(input_luminance), input_color, chromatic_adaptation);
329 float4 adaptation_level = math::interpolate(
330 global_adaptation_level, local_adaptation_level, light_adaptation);
331
332 /* Equation (1) from Reinhard's 2005 paper, assuming `Vmax` is 1. */
333 float4 semi_saturation = math::pow(intensity * adaptation_level, contrast);
334 float4 tone_mapped_color = math::safe_divide(input_color, input_color + semi_saturation);
335
336 output.store_pixel(texel, Color(float4(tone_mapped_color.xyz(), input_color.w)));
337 });
338 }
339
340 /* Computes the global adaptation level from the trilinear interpolation equations constructed
341 * from equations (6) and (7) in Reinhard's 2005 paper. */
343 {
344 const Result &input = this->get_input("Image");
345 const float4 mean_color = sum_color(context(), input) /
347
348 float luminance_coefficients[3];
350 const float mean_luminance = math::dot(mean_color.xyz(), float3(luminance_coefficients));
351
352 const float chromatic_adaptation = this->get_chromatic_adaptation();
353 return math::interpolate(float4(mean_luminance), mean_color, chromatic_adaptation);
354 }
355
356 /* Computes equation (5) from Reinhard's 2005 paper. */
358 {
359 return std::exp(-this->get_intensity());
360 }
361
362 /* If the contrast is not zero, return it, otherwise, a zero contrast denote automatic derivation
363 * of the contrast value based on equations (2) and (4) from Reinhard's 2005 paper. */
365 {
366 if (this->get_contrast() != 0.0f) {
367 return this->get_contrast();
368 }
369
370 const float log_maximum_luminance = compute_log_maximum_luminance();
371 const float log_minimum_luminance = compute_log_minimum_luminance();
372
373 /* This is merely to guard against zero division later. */
374 if (log_maximum_luminance == log_minimum_luminance) {
375 return 1.0f;
376 }
377
378 const float average_log_luminance = compute_average_log_luminance();
379 const float dynamic_range = log_maximum_luminance - log_minimum_luminance;
380 const float luminance_key = (log_maximum_luminance - average_log_luminance) / (dynamic_range);
381
382 return 0.3f + 0.7f * std::pow(luminance_key, 1.4f);
383 }
384
386 {
387 const Result &input_image = get_input("Image");
388
389 float luminance_coefficients[3];
391 const float sum_of_log_luminance = sum_log_luminance(
392 context(), input_image, luminance_coefficients);
393
394 return sum_of_log_luminance /
395 (input_image.domain().data_size.x * input_image.domain().data_size.y);
396 }
397
399 {
400 float luminance_coefficients[3];
402 const float maximum = maximum_luminance(context(), get_input("Image"), luminance_coefficients);
403 return std::log(math::max(maximum, 1e-5f));
404 }
405
407 {
408 float luminance_coefficients[3];
410 const float minimum = minimum_luminance(context(), get_input("Image"), luminance_coefficients);
411 return std::log(math::max(minimum, 1e-5f));
412 }
413
415 {
416 return this->get_input("Intensity").get_single_value_default<float>();
417 }
418
420 {
421 return math::max(0.0f, this->get_input("Contrast").get_single_value_default<float>());
422 }
423
425 {
426 return math::clamp(
427 this->get_input("Chromatic Adaptation").get_single_value_default<float>(), 0.0f, 1.0f);
428 }
429
431 {
432 return math::clamp(
433 this->get_input("Light Adaptation").get_single_value_default<float>(), 0.0f, 1.0f);
434 }
435
437 {
438 return CMPNodeToneMapType(this->get_input("Type").get_single_value_default<MenuValue>().value);
439 }
440};
441
443{
444 return new ToneMapOperation(context, node);
445}
446
447static void node_register()
448{
449 static bke::bNodeType ntype;
450
451 cmp_node_type_base(&ntype, "CompositorNodeTonemap"_ustr, CMP_NODE_TONEMAP);
452 ntype.ui_name = "Tonemap";
453 ntype.ui_description =
454 "Map one set of colors to another in order to approximate the appearance of high dynamic "
455 "range";
456 ntype.enum_name_legacy = "TONEMAP";
458 ntype.declare = node_declare;
459 ntype.initfunc = node_init;
463
465}
466NOD_REGISTER_NODE(node_register)
467
468} // namespace blender::nodes::node_composite_tone_map_cc
#define NODE_CLASS_OP_COLOR
Definition BKE_node.hh:480
#define CMP_NODE_TONEMAP
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define output
gpu::Shader * get_shader(const char *info_name, ResultPrecision precision)
NodeOperation(Context &context, const bNode &node)
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
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
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
float minimum_luminance(Context &context, const Result &input, const float3 &luminance_coefficients)
float sum_log_luminance(Context &context, const Result &input, const float3 &luminance_coefficients)
void parallel_for(const int2 range, const Function &function)
float maximum_luminance(Context &context, const Result &input, const float3 &luminance_coefficients)
float4 sum_color(Context &context, const Result &input)
T clamp(const T &a, const T &min, const T &max)
T dot(const QuaternionBase< T > &a, const QuaternionBase< T > &b)
T interpolate(const T &a, const T &b, const FactorT &t)
T reduce_mul(const VecBase< T, Size > &a)
T max(const T &a, const T &b)
static NodeOperation * get_compositor_operation(Context &context, const bNode &node)
static void node_declare(NodeDeclarationBuilder &b)
static void node_init(bNodeTree *, bNode *node)
@ CMP_NODE_TONE_MAP_SIMPLE
@ CMP_NODE_TONE_MAP_PHOTORECEPTOR
BLI_INLINE void IMB_colormanagement_get_luminance_coefficients(float r_rgb[3])
void GPU_shader_uniform_4fv(gpu::Shader *sh, const char *name, const float data[4])
void GPU_shader_unbind()
void GPU_shader_uniform_3fv(gpu::Shader *sh, const char *name, const float data[3])
void GPU_shader_bind(gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants_state=nullptr)
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)
Definition node_util.cc:57
void node_free_standard_storage(bNode *node)
Definition node_util.cc:45
@ 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
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
#define N_(msgid)