Blender V4.3
COM_CalculateMeanOperation.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
6
8
10
11namespace blender::compositor {
12
14{
17 is_calculated_ = false;
18 setting_ = 1;
19
20 /* Prevent optimization by constant folder. */
22
24}
25
30
32{
33 setting_ = setting;
34 switch (setting) {
35 case 1: {
37 break;
38 }
39 case 2: {
40 setting_func_ = [](const float *elem) { return elem[0]; };
41 break;
42 }
43 case 3: {
44 setting_func_ = [](const float *elem) { return elem[1]; };
45 break;
46 }
47 case 4: {
48 setting_func_ = [](const float *elem) { return elem[2]; };
49 break;
50 }
51 case 5: {
52 setting_func_ = [](const float *elem) {
53 float yuv[3];
54 rgb_to_yuv(elem[0], elem[1], elem[2], &yuv[0], &yuv[1], &yuv[2], BLI_YUV_ITU_BT709);
55 return yuv[0];
56 };
57 break;
58 }
59 }
60}
61
63 const rcti & /*output_area*/,
64 rcti &r_input_area)
65{
66 BLI_assert(input_idx == 0);
67 r_input_area = get_input_operation(input_idx)->get_canvas();
68}
69
70void CalculateMeanOperation::determine_canvas(const rcti &preferred_area, rcti &r_area)
71{
72 ConstantOperation::determine_canvas(preferred_area, r_area);
73 r_area = preferred_area;
74}
75
77{
78 /* Node de-duplication uses the constant value as part of a hash for constant operations.
79 * The constant is not known in advance here, but need to return something. The value does
80 * not really matter, because if two CalculateMean operations are connected to different
81 * inputs it will be handled via hash of the input subtree. */
82 static float f = 0;
83 return &f;
84}
85
87 const rcti &area,
89{
90 if (!is_calculated_) {
91 MemoryBuffer *input = inputs[0];
93 is_calculated_ = true;
94 }
95
96 output->fill(area, &constant_value_);
97}
98
100{
101 return calculate_mean(input);
102}
103
105{
106 PixelsSum total = {0};
108 input->get_rect(),
109 [=](const rcti &split) { return calc_area_sum(input, split); },
110 total,
111 [](PixelsSum &join, const PixelsSum &chunk) {
112 join.sum += chunk.sum;
113 join.num_pixels += chunk.num_pixels;
114 });
115 return total.num_pixels == 0 ? 0.0f : total.sum / total.num_pixels;
116}
117
119PixelsSum CalculateMeanOperation::calc_area_sum(const MemoryBuffer *input, const rcti &area) const
120{
121 PixelsSum result = {0};
122 for (const float *elem : input->get_buffer_area(area)) {
123 if (elem[3] <= 0.0f) {
124 continue;
125 }
126 result.sum += setting_func_(elem);
127 result.num_pixels++;
128 }
129 return result;
130}
131
132} // namespace blender::compositor
#define BLI_assert(a)
Definition BLI_assert.h:50
#define BLI_YUV_ITU_BT709
void rgb_to_yuv(float r, float g, float b, float *r_y, float *r_u, float *r_v, int colorspace)
Definition math_color.cc:67
static void split(const char *text, const char *seps, char ***str, int *count)
BLI_INLINE float IMB_colormanagement_get_luminance(const float rgb[3])
void update_memory_buffer(MemoryBuffer *output, const rcti &area, Span< MemoryBuffer * > inputs) override
float calculate_mean(const MemoryBuffer *input) const
std::function< float(const float *elem)> setting_func_
void get_area_of_interest(int input_idx, const rcti &output_area, rcti &r_input_area) override
Get input operation area being read by this operation on rendering given output area.
void determine_canvas(const rcti &preferred_area, rcti &r_area) override
virtual float calculate_value(const MemoryBuffer *input) const
void execute_work(const rcti &work_rect, std::function< void(const rcti &split_rect)> work_func)
a MemoryBuffer contains access to the data
void add_output_socket(DataType datatype)
NodeOperation * get_input_operation(int index)
void add_input_socket(DataType datatype, ResizeMode resize_mode=ResizeMode::Center)
virtual void determine_canvas(const rcti &preferred_area, rcti &r_area)