Blender V5.3
MOD_tonemap.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_array.hh"
10
11#include "BLT_translation.hh"
12
13#include "DNA_sequence_types.h"
14
16
17#include "PRF_profile.hh"
18
19#include "SEQ_modifier.hh"
20#include "SEQ_render.hh"
21
22#include "UI_interface.hh"
24
25#include "RNA_access.hh"
26
27#include "modifier.hh"
28#include "render.hh"
29
30namespace blender::seq {
31
32struct AvgLogLum {
34 float al;
35 float auto_key;
36 float lav;
38 float igm;
39};
40
42{
43 SequencerTonemapModifierData *tmmd = reinterpret_cast<SequencerTonemapModifierData *>(smd);
44 /* Same as tone-map compositor node. */
46 tmmd->key = 0.18f;
47 tmmd->offset = 1.0f;
48 tmmd->gamma = 1.0f;
49 tmmd->intensity = 0.0f;
50 tmmd->contrast = 0.0f;
51 tmmd->adaptation = 1.0f;
52 tmmd->correction = 0.0f;
53}
54
55/* Convert chunk of float image pixels to scene linear space, in-place. */
57 float4 *pixels,
58 int64_t count)
59{
61 reinterpret_cast<float *>(pixels), int(count), 1, 4, colorspace, false);
62}
63
64/* Convert chunk of byte image pixels to scene linear space, into a destination array. */
66 const uchar *pixels,
67 float4 *dst,
68 int64_t count)
69{
70 const uchar *bptr = pixels;
71 float4 *dst_ptr = dst;
72 for (int64_t i = 0; i < count; i++) {
73 straight_uchar_to_premul_float(*dst_ptr, bptr);
74 bptr += 4;
75 dst_ptr++;
76 }
78 reinterpret_cast<float *>(dst), int(count), 1, 4, colorspace, false);
79}
80
82{
85 reinterpret_cast<float *>(src), int(range.size()), 1, 4, colorspace);
86 const float4 *src_ptr = src;
87 uchar *bptr = ibuf->byte_data_for_write();
88 for (const int64_t idx : range) {
89 premul_float_to_straight_uchar(bptr + idx * 4, *src_ptr);
90 src_ptr++;
91 }
92}
93
96 double sum = 0.0f;
97 float3 color_sum = {0, 0, 0};
98 double log_sum = 0.0;
99 float min = FLT_MAX;
100 float max = -FLT_MAX;
101};
102
104{
106 float4 *fptr = reinterpret_cast<float4 *>(ibuf->float_data_for_write());
108 reinterpret_cast<float *>(fptr + range.first()), int(range.size()), 1, 4, colorspace);
109}
110
111template<typename MaskSampler>
112static void tonemap_simple(
113 float4 *scene_linear, MaskSampler &mask, int image_x, IndexRange y_range, const AvgLogLum &avg)
114{
115 for (int64_t y : y_range) {
116 mask.begin_row(y);
117 for ([[maybe_unused]] int64_t x : IndexRange(image_x)) {
118 float4 input = *scene_linear;
119
120 /* Apply correction. */
121 float3 pixel = input.xyz() * avg.al;
122 float3 d = pixel + avg.tmmd->offset;
123 pixel.x /= (d.x == 0.0f) ? 1.0f : d.x;
124 pixel.y /= (d.y == 0.0f) ? 1.0f : d.y;
125 pixel.z /= (d.z == 0.0f) ? 1.0f : d.z;
126 const float igm = avg.igm;
127 if (igm != 0.0f) {
128 pixel.x = powf(math::max(pixel.x, 0.0f), igm);
129 pixel.y = powf(math::max(pixel.y, 0.0f), igm);
130 pixel.z = powf(math::max(pixel.z, 0.0f), igm);
131 }
132
133 /* Apply mask. */
134 float4 result(pixel.x, pixel.y, pixel.z, input.w);
135 mask.apply_mask(input, result);
136 *scene_linear = result;
137 scene_linear++;
138 }
139 }
140}
141
142template<typename MaskSampler>
144 float4 *scene_linear, MaskSampler &mask, int image_x, IndexRange y_range, const AvgLogLum &avg)
145{
146 const float f = expf(-avg.tmmd->intensity);
147 const float m = (avg.tmmd->contrast > 0.0f) ? avg.tmmd->contrast :
148 (0.3f + 0.7f * powf(avg.auto_key, 1.4f));
149 const float ic = 1.0f - avg.tmmd->correction, ia = 1.0f - avg.tmmd->adaptation;
150
151 for (int64_t y : y_range) {
152 mask.begin_row(y);
153 for ([[maybe_unused]] int64_t x : IndexRange(image_x)) {
154 float4 input = *scene_linear;
155
156 /* Apply correction. */
157 float3 pixel = input.xyz();
158 const float L = IMB_colormanagement_get_luminance(pixel);
159 float I_l = pixel.x + ic * (L - pixel.x);
160 float I_g = avg.cav.x + ic * (avg.lav - avg.cav.x);
161 float I_a = I_l + ia * (I_g - I_l);
162 pixel.x /= std::max(pixel.x + powf(f * I_a, m), 1.0e-30f);
163 I_l = pixel.y + ic * (L - pixel.y);
164 I_g = avg.cav.y + ic * (avg.lav - avg.cav.y);
165 I_a = I_l + ia * (I_g - I_l);
166 pixel.y /= std::max(pixel.y + powf(f * I_a, m), 1.0e-30f);
167 I_l = pixel.z + ic * (L - pixel.z);
168 I_g = avg.cav.z + ic * (avg.lav - avg.cav.z);
169 I_a = I_l + ia * (I_g - I_l);
170 pixel.z /= std::max(pixel.z + powf(f * I_a, m), 1.0e-30f);
171
172 /* Apply mask. */
173 float4 result(pixel.x, pixel.y, pixel.z, input.w);
174 mask.apply_mask(input, result);
175 *scene_linear = result;
176 scene_linear++;
177 }
178 }
179}
180
186
187 template<typename ImageT, typename MaskSampler>
188 void apply(ImageT *image, MaskSampler &mask, int image_x, IndexRange y_range)
189 {
190 const IndexRange pixel_range(y_range.first() * image_x, y_range.size() * image_x);
191 if constexpr (std::is_same_v<ImageT, float>) {
192 /* Float pixels: no need for temporary storage. Luminance calculation already converted
193 * data to scene linear. */
194 float4 *pixels = (float4 *)(image + y_range.first() * image_x * 4);
195 if (this->type == SEQ_TONEMAP_RD_PHOTORECEPTOR) {
196 tonemap_rd_photoreceptor(pixels, mask, image_x, y_range, data);
197 }
198 else {
199 BLI_assert(this->type == SEQ_TONEMAP_RH_SIMPLE);
200 tonemap_simple(pixels, mask, image_x, y_range, data);
201 }
202 scene_linear_to_image_chunk_float(this->ibuf, pixel_range);
203 }
204 else {
205 /* Byte pixels: temporary storage for scene linear pixel values. */
206 Array<float4> scene_linear(pixel_range.size());
208 ibuf->byte_data() + pixel_range.first() * 4,
209 scene_linear.data(),
210 pixel_range.size());
211 if (this->type == SEQ_TONEMAP_RD_PHOTORECEPTOR) {
212 tonemap_rd_photoreceptor(scene_linear.data(), mask, image_x, y_range, data);
213 }
214 else {
215 BLI_assert(this->type == SEQ_TONEMAP_RH_SIMPLE);
216 tonemap_simple(scene_linear.data(), mask, image_x, y_range, data);
217 }
218 scene_linear_to_image_chunk_byte(scene_linear.data(), this->ibuf, pixel_range);
219 }
220 }
221};
222
223static void tonemap_calc_chunk_luminance(const int width,
224 const IndexRange y_range,
225 const float4 *scene_linear,
226 AreaLuminance &r_lum)
227{
228 for ([[maybe_unused]] const int y : y_range) {
229 for (int x = 0; x < width; x++) {
230 float4 pixel = *scene_linear;
231 r_lum.pixel_count++;
233 r_lum.sum += L;
234 r_lum.color_sum.x += pixel.x;
235 r_lum.color_sum.y += pixel.y;
236 r_lum.color_sum.z += pixel.z;
237 r_lum.log_sum += logf(math::max(L, 0.0f) + 1e-5f);
238 r_lum.max = math::max(r_lum.max, L);
239 r_lum.min = math::min(r_lum.min, L);
240 scene_linear++;
241 }
242 }
243}
244
246{
247 float *float_data = ibuf->float_data_for_write();
248 AreaLuminance lum;
250 IndexRange(ibuf->y),
251 32,
252 lum,
253 /* Calculate luminance for a chunk. */
254 [&](const IndexRange y_range, const AreaLuminance &init) {
255 AreaLuminance lum = init;
256 const int64_t chunk_size = y_range.size() * ibuf->x;
257 /* For float images, convert to scene-linear in place. The rest
258 * of tone-mapper can then continue with scene-linear values. */
259 if (float_data != nullptr) {
260 float4 *fptr = reinterpret_cast<float4 *>(float_data);
261 fptr += y_range.first() * ibuf->x;
262 pixels_to_scene_linear_float(ibuf->float_buffer.colorspace, fptr, chunk_size);
263 tonemap_calc_chunk_luminance(ibuf->x, y_range, fptr, lum);
264 }
265 else {
266 const uchar *bptr = ibuf->byte_data() + y_range.first() * ibuf->x * 4;
267 Array<float4> scene_linear(chunk_size);
268 pixels_to_scene_linear_byte(
269 ibuf->byte_buffer.colorspace, bptr, scene_linear.data(), chunk_size);
270 tonemap_calc_chunk_luminance(ibuf->x, y_range, scene_linear.data(), lum);
271 }
272 return lum;
273 },
274 /* Reduce luminance results. */
275 [&](const AreaLuminance &a, const AreaLuminance &b) {
276 AreaLuminance res;
277 res.pixel_count = a.pixel_count + b.pixel_count;
278 res.sum = a.sum + b.sum;
279 res.color_sum = a.color_sum + b.color_sum;
280 res.log_sum = a.log_sum + b.log_sum;
281 res.min = math::min(a.min, b.min);
282 res.max = math::max(a.max, b.max);
283 return res;
284 });
285 return lum;
286}
287
289{
291 ensure_ibuf_is_sequencer_space(context.render_data.scene, context.result.image, false);
292 ImBuf *mask = modifier_render_mask_input(context, *smd);
293
294 const SequencerTonemapModifierData *tmmd =
295 reinterpret_cast<const SequencerTonemapModifierData *>(smd);
296
298 op.type = tmmd->type;
299 op.ibuf = context.result.image;
300 op.lum = tonemap_calc_input_luminance(context.result.image);
301 if (op.lum.pixel_count == 0) {
302 return; /* Strip is zero size or off-screen. */
303 }
304
305 op.data.tmmd = tmmd;
306 op.data.lav = op.lum.sum / op.lum.pixel_count;
307 op.data.cav.x = op.lum.color_sum.x / op.lum.pixel_count;
308 op.data.cav.y = op.lum.color_sum.y / op.lum.pixel_count;
309 op.data.cav.z = op.lum.color_sum.z / op.lum.pixel_count;
310 float maxl = log(double(op.lum.max) + 1e-5f);
311 float minl = log(double(op.lum.min) + 1e-5f);
312 float avl = op.lum.log_sum / op.lum.pixel_count;
313 op.data.auto_key = (maxl > minl) ? ((maxl - avl) / (maxl - minl)) : 1.0f;
314 float al = exp(double(avl));
315 op.data.al = (al == 0.0f) ? 0.0f : (tmmd->key / al);
316 op.data.igm = (tmmd->gamma == 0.0f) ? 1.0f : (1.0f / tmmd->gamma);
317
318 apply_modifier_op(op, context.result.image, mask, context.transform);
319
320 if (mask != nullptr) {
322 }
323}
324
325static void tonemapmodifier_panel_draw(const bContext *C, Panel *panel)
326{
327 ui::Layout &layout = *panel->layout;
329
330 const int tonemap_type = RNA_enum_get(ptr, "tonemap_type");
331
332 layout.use_property_split_set(true);
333
334 ui::Layout &col = layout.column(false);
335 col.prop(ptr, "tonemap_type", UI_ITEM_NONE, std::nullopt, ICON_NONE);
336 if (tonemap_type == SEQ_TONEMAP_RD_PHOTORECEPTOR) {
337 col.prop(ptr, "intensity", UI_ITEM_NONE, std::nullopt, ICON_NONE);
338 col.prop(ptr, "contrast", UI_ITEM_NONE, std::nullopt, ICON_NONE);
339 col.prop(ptr, "adaptation", UI_ITEM_NONE, std::nullopt, ICON_NONE);
340 col.prop(ptr, "correction", UI_ITEM_NONE, std::nullopt, ICON_NONE);
341 }
342 else if (tonemap_type == SEQ_TONEMAP_RH_SIMPLE) {
343 col.prop(ptr, "key", UI_ITEM_NONE, std::nullopt, ICON_NONE);
344 col.prop(ptr, "offset", UI_ITEM_NONE, std::nullopt, ICON_NONE);
345 col.prop(ptr, "gamma", UI_ITEM_NONE, std::nullopt, ICON_NONE);
346 }
347 else {
349 }
350
351 if (ui::Layout *mask_input_layout = layout.panel_prop(
352 C, ptr, "open_mask_input_panel", IFACE_("Mask Input")))
353 {
354 draw_mask_input_type_settings(C, *mask_input_layout, ptr);
355 }
356}
357
362
364 /*idname*/ "Tonemap",
365 /*name*/ CTX_N_(BLT_I18NCONTEXT_ID_SEQUENCE, "Tonemap"),
366 /*struct_name*/ "SequencerTonemapModifierData",
367 /*struct_size*/ sizeof(SequencerTonemapModifierData),
368 /*init_data*/ tonemapmodifier_init_data,
369 /*free_data*/ nullptr,
370 /*copy_data*/ nullptr,
371 /*apply*/ tonemapmodifier_apply,
372 /*panel_register*/ tonemapmodifier_register,
373 /*blend_write*/ nullptr,
374 /*blend_read*/ nullptr,
375};
376
377}; // namespace blender::seq
#define CTX_N_(context, msgid)
#define BLT_I18NCONTEXT_ID_SEQUENCE
#define IFACE_(msgid)
#define PRF_scope_with_name(ui_name, category)
#define C
Definition RandGen.cpp:29
#define UI_ITEM_NONE
#define BLI_assert_unreachable()
const T * data() const
Definition BLI_array.hh:323
constexpr int64_t first() const
constexpr int64_t size() const
constexpr IndexRange()=default
#define logf(x)
#define expf(x)
#define powf(x, y)
#define FLT_MAX
#define log
#define exp
#define L
T min(const T &a, const T &b)
T max(const T &a, const T &b)
static void scene_linear_to_image_chunk_float(ImBuf *ibuf, IndexRange range)
ImBuf * modifier_render_mask_input(const ModifierApplyContext &context, const StripModifierData &smd)
void ensure_ibuf_is_sequencer_space(const Scene *scene, ImBuf *ibuf, bool make_float)
Definition render.cc:170
static void tonemap_calc_chunk_luminance(const int width, const IndexRange y_range, const float4 *scene_linear, AreaLuminance &r_lum)
void apply_modifier_op(T &op, ImBuf *ibuf, const ImBuf *mask, const float3x3 &mask_transform)
Definition modifier.hh:277
static void tonemapmodifier_init_data(StripModifierData *smd)
static void tonemapmodifier_panel_draw(const bContext *C, Panel *panel)
StripModifierTypeInfo seqModifierType_Tonemap
static AreaLuminance tonemap_calc_input_luminance(ImBuf *ibuf)
static void tonemap_simple(float4 *scene_linear, MaskSampler &mask, int image_x, IndexRange y_range, const AvgLogLum &avg)
static void pixels_to_scene_linear_float(const ColorSpace *colorspace, float4 *pixels, int64_t count)
static void tonemapmodifier_apply(ModifierApplyContext &context, StripModifierData *smd)
static void tonemapmodifier_register(ARegionType *region_type)
void draw_mask_input_type_settings(const bContext *C, ui::Layout &layout, PointerRNA *ptr)
static void pixels_to_scene_linear_byte(const ColorSpace *colorspace, const uchar *pixels, float4 *dst, int64_t count)
static void scene_linear_to_image_chunk_byte(float4 *src, ImBuf *ibuf, IndexRange range)
PanelType * modifier_panel_register(ARegionType *region_type, const eStripModifierType type, PanelDrawFn draw)
static void tonemap_rd_photoreceptor(float4 *scene_linear, MaskSampler &mask, int image_x, IndexRange y_range, const AvgLogLum &avg)
Value parallel_reduce(IndexRange range, int64_t grain_size, const Value &identity, const Function &function, const Reduction &reduction)
Definition BLI_task.hh:127
PointerRNA * panel_custom_data_get(const Panel *panel)
@ SEQ_TONEMAP_RD_PHOTORECEPTOR
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
__int64 int64_t
Definition BKE_text.h:90
int RNA_enum_get(PointerRNA *ptr, const char *name)
void IMB_colormanagement_scene_linear_to_colorspace(float *buffer, int width, int height, int channels, const ColorSpace *colorspace)
VecBase< float, 4 > float4
void IMB_freeImBuf(ImBuf *ibuf)
unsigned char uchar
Definition BKE_text.h:58
MINLINE void straight_uchar_to_premul_float(float result[4], const unsigned char color[4])
BLI_INLINE float IMB_colormanagement_get_luminance(const float rgb[3])
VecBase< float, 3 > float3
MINLINE void premul_float_to_straight_uchar(unsigned char *result, const float color[4])
void IMB_colormanagement_colorspace_to_scene_linear(float *buffer, int width, int height, int channels, const ColorSpace *colorspace, bool predivide)
ocio::ColorSpace ColorSpace
Definition BLF_api.hh:42
const ColorSpace * colorspace
const ColorSpace * colorspace
ImBufFloatBuffer float_buffer
ImBufByteBuffer byte_buffer
const uint8_t * byte_data() const
uint8_t * byte_data_for_write()
Definition allocimbuf.cc:60
float * float_data_for_write()
Definition allocimbuf.cc:79
ui::Layout * layout
const SequencerTonemapModifierData * tmmd
void apply(ImageT *image, MaskSampler &mask, int image_x, IndexRange y_range)
void use_property_split_set(bool is_sep)
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)
Layout & column(bool align)
PanelLayout panel_prop(const bContext *C, PointerRNA *open_prop_owner, StringRefNull open_prop_name)