Blender V4.3
COM_SummedAreaTableOperation.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BLI_math_vector.hh"
7#include "BLI_task.hh"
8
10
11namespace blender::compositor {
12
22
24 const rcti & /*output_area*/,
25 rcti &r_input_area)
26{
27 r_input_area = get_input_operation(input_idx)->get_canvas();
28}
29
31 const rcti &area,
33{
34 /* NOTE: although this is a single threaded call, multithreading is used. */
35 MemoryBuffer *image = inputs[0];
36
37 /* First pass: copy input to output and sum horizontally. */
39 IndexRange(area.ymin, area.ymax - area.ymin), 1, [&](const IndexRange range_y) {
40 for (const int y : range_y) {
41 float4 accumulated_color = float4(0.0f);
42 for (const int x : IndexRange(area.xmin, area.xmax - area.xmin)) {
43 const float4 color = float4(image->get_elem(x, y));
44 accumulated_color += mode_ == eMode::Squared ? color * color : color;
45 copy_v4_v4(output->get_elem(x, y), accumulated_color);
46 }
47 }
48 });
49
50 /* Second pass: vertical sum. */
52 IndexRange(area.xmin, area.xmax - area.xmin), 1, [&](const IndexRange range_x) {
53 for (const int x : range_x) {
54 float4 accumulated_color = float4(0.0f);
55 for (const int y : IndexRange(area.ymin, area.ymax - area.ymin)) {
56 const float4 color = float4(output->get_elem(x, y));
57 accumulated_color += color;
58 copy_v4_v4(output->get_elem(x, y), accumulated_color);
59 }
60 }
61 });
62}
63
64void SummedAreaTableOperation::set_mode(eMode mode)
65{
66 mode_ = mode;
67}
68
69SummedAreaTableOperation::eMode SummedAreaTableOperation::get_mode()
70{
71 return mode_;
72}
73
75{
76 /*
77 * a, b, c and d are the bounding box of the given area. They are defined as follows:
78 *
79 * y
80 * ▲
81 * │
82 * ├──────x───────x
83 * │ │c d│
84 * ├──────x───────x
85 * │ │a b│
86 * └──────┴───────┴──────► x
87 *
88 * NOTE: this is the same definition as in https://en.wikipedia.org/wiki/Summed-area_table
89 * but using the blender convention with the origin being at the lower left.
90 */
91
92 BLI_assert(area.xmin <= area.xmax && area.ymin <= area.ymax);
93
94 int2 lower_bound(area.xmin, area.ymin);
95 int2 upper_bound(area.xmax, area.ymax);
96
97 int2 corrected_lower_bound = lower_bound - int2(1, 1);
98 int2 corrected_upper_bound;
99 corrected_upper_bound[0] = math::min(buffer->get_width() - 1, upper_bound[0]);
100 corrected_upper_bound[1] = math::min(buffer->get_height() - 1, upper_bound[1]);
101
102 float4 a, b, c, d, addend, substrahend;
103 buffer->read_elem_checked(corrected_upper_bound[0], corrected_upper_bound[1], a);
104 buffer->read_elem_checked(corrected_lower_bound[0], corrected_lower_bound[1], d);
105 addend = a + d;
106
107 buffer->read_elem_checked(corrected_lower_bound[0], corrected_upper_bound[1], b);
108 buffer->read_elem_checked(corrected_upper_bound[0], corrected_lower_bound[1], c);
109 substrahend = b + c;
110
111 float4 sum = addend - substrahend;
112
113 return sum;
114}
115
116} // namespace blender::compositor
#define BLI_assert(a)
Definition BLI_assert.h:50
static T sum(const btAlignedObjectArray< T > &items)
a MemoryBuffer contains access to the data
void read_elem_checked(int x, int y, float *out) const
const int get_width() const
get the width of this MemoryBuffer
const int get_height() const
get the height of this MemoryBuffer
void add_output_socket(DataType datatype)
NodeOperation * get_input_operation(int index)
void add_input_socket(DataType datatype, ResizeMode resize_mode=ResizeMode::Center)
void update_memory_buffer(MemoryBuffer *output, const rcti &area, Span< MemoryBuffer * > inputs) override
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.
local_group_size(16, 16) .push_constant(Type b
float4 summed_area_table_sum(MemoryBuffer *buffer, const rcti &area)
T min(const T &a, const T &b)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:95
VecBase< int32_t, 2 > int2