diff options
Diffstat (limited to 'source/blender/compositor/operations')
4 files changed, 173 insertions, 6 deletions
diff --git a/source/blender/compositor/operations/COM_CalculateMeanOperation.cc b/source/blender/compositor/operations/COM_CalculateMeanOperation.cc index a7ea49aed8d..7457ac9e227 100644 --- a/source/blender/compositor/operations/COM_CalculateMeanOperation.cc +++ b/source/blender/compositor/operations/COM_CalculateMeanOperation.cc @@ -19,6 +19,7 @@ #include "COM_CalculateMeanOperation.h" #include "BLI_math.h" #include "BLI_utildefines.h" +#include "COM_ExecutionSystem.h" #include "IMB_colormanagement.h" @@ -128,4 +129,93 @@ void CalculateMeanOperation::calculateMean(MemoryBuffer *tile) this->m_result = sum / pixels; } +void CalculateMeanOperation::setSetting(int setting) +{ + this->m_setting = setting; + switch (setting) { + case 1: { + setting_func_ = IMB_colormanagement_get_luminance; + break; + } + case 2: { + setting_func_ = [](const float *elem) { return elem[0]; }; + break; + } + case 3: { + setting_func_ = [](const float *elem) { return elem[1]; }; + break; + } + case 4: { + setting_func_ = [](const float *elem) { return elem[2]; }; + break; + } + case 5: { + setting_func_ = [](const float *elem) { + float yuv[3]; + rgb_to_yuv(elem[0], elem[1], elem[2], &yuv[0], &yuv[1], &yuv[2], BLI_YUV_ITU_BT709); + return yuv[0]; + }; + break; + } + } +} + +void CalculateMeanOperation::get_area_of_interest(int input_idx, + const rcti &UNUSED(output_area), + rcti &r_input_area) +{ + BLI_assert(input_idx == 0); + NodeOperation *operation = getInputOperation(input_idx); + r_input_area.xmin = 0; + r_input_area.ymin = 0; + r_input_area.xmax = operation->getWidth(); + r_input_area.ymax = operation->getHeight(); +} + +void CalculateMeanOperation::update_memory_buffer_started(MemoryBuffer *UNUSED(output), + const rcti &UNUSED(area), + Span<MemoryBuffer *> inputs) +{ + if (!this->m_iscalculated) { + MemoryBuffer *input = inputs[0]; + m_result = calc_mean(input); + this->m_iscalculated = true; + } +} + +void CalculateMeanOperation::update_memory_buffer_partial(MemoryBuffer *output, + const rcti &area, + Span<MemoryBuffer *> UNUSED(inputs)) +{ + output->fill(area, &m_result); +} + +float CalculateMeanOperation::calc_mean(const MemoryBuffer *input) +{ + PixelsSum total = {0}; + exec_system_->execute_work<PixelsSum>( + input->get_rect(), + [=](const rcti &split) { return calc_area_sum(input, split); }, + total, + [](PixelsSum &join, const PixelsSum &chunk) { + join.sum += chunk.sum; + join.num_pixels += chunk.num_pixels; + }); + return total.num_pixels == 0 ? 0.0f : total.sum / total.num_pixels; +} + +using PixelsSum = CalculateMeanOperation::PixelsSum; +PixelsSum CalculateMeanOperation::calc_area_sum(const MemoryBuffer *input, const rcti &area) +{ + PixelsSum result = {0}; + for (const float *elem : input->get_buffer_area(area)) { + if (elem[3] <= 0.0f) { + continue; + } + result.sum += setting_func_(elem); + result.num_pixels++; + } + return result; +} + } // namespace blender::compositor diff --git a/source/blender/compositor/operations/COM_CalculateMeanOperation.h b/source/blender/compositor/operations/COM_CalculateMeanOperation.h index 8b3bf281c93..779ca79b38a 100644 --- a/source/blender/compositor/operations/COM_CalculateMeanOperation.h +++ b/source/blender/compositor/operations/COM_CalculateMeanOperation.h @@ -18,8 +18,9 @@ #pragma once -#include "COM_NodeOperation.h" +#include "COM_MultiThreadedOperation.h" #include "DNA_node_types.h" +#include <functional> namespace blender::compositor { @@ -27,7 +28,13 @@ namespace blender::compositor { * \brief base class of CalculateMean, implementing the simple CalculateMean * \ingroup operation */ -class CalculateMeanOperation : public NodeOperation { +class CalculateMeanOperation : public MultiThreadedOperation { + public: + struct PixelsSum { + float sum; + int num_pixels; + }; + protected: /** * \brief Cached reference to the reader @@ -37,6 +44,7 @@ class CalculateMeanOperation : public NodeOperation { bool m_iscalculated; float m_result; int m_setting; + std::function<float(const float *elem)> setting_func_; public: CalculateMeanOperation(); @@ -61,13 +69,24 @@ class CalculateMeanOperation : public NodeOperation { bool determineDependingAreaOfInterest(rcti *input, ReadBufferOperation *readOperation, rcti *output) override; - void setSetting(int setting) - { - this->m_setting = setting; - } + void setSetting(int setting); + + void get_area_of_interest(int input_idx, const rcti &output_area, rcti &r_input_area) override; + + virtual void update_memory_buffer_started(MemoryBuffer *output, + const rcti &area, + Span<MemoryBuffer *> inputs) override; + + virtual void update_memory_buffer_partial(MemoryBuffer *output, + const rcti &area, + Span<MemoryBuffer *> inputs) override; protected: void calculateMean(MemoryBuffer *tile); + float calc_mean(const MemoryBuffer *input); + + private: + PixelsSum calc_area_sum(const MemoryBuffer *input, const rcti &area); }; } // namespace blender::compositor diff --git a/source/blender/compositor/operations/COM_CalculateStandardDeviationOperation.cc b/source/blender/compositor/operations/COM_CalculateStandardDeviationOperation.cc index ed554b9ac06..494b66cb888 100644 --- a/source/blender/compositor/operations/COM_CalculateStandardDeviationOperation.cc +++ b/source/blender/compositor/operations/COM_CalculateStandardDeviationOperation.cc @@ -19,6 +19,7 @@ #include "COM_CalculateStandardDeviationOperation.h" #include "BLI_math.h" #include "BLI_utildefines.h" +#include "COM_ExecutionSystem.h" #include "IMB_colormanagement.h" @@ -96,4 +97,50 @@ void *CalculateStandardDeviationOperation::initializeTileData(rcti *rect) return nullptr; } +void CalculateStandardDeviationOperation::update_memory_buffer_started( + MemoryBuffer *UNUSED(output), const rcti &UNUSED(area), Span<MemoryBuffer *> inputs) +{ + if (!this->m_iscalculated) { + const MemoryBuffer *input = inputs[0]; + const float mean = CalculateMeanOperation::calc_mean(input); + + PixelsSum total = {0}; + exec_system_->execute_work<PixelsSum>( + input->get_rect(), + [=](const rcti &split) { return calc_area_sum(input, split, mean); }, + total, + [](PixelsSum &join, const PixelsSum &chunk) { + join.sum += chunk.sum; + join.num_pixels += chunk.num_pixels; + }); + this->m_standardDeviation = total.num_pixels <= 1 ? + 0.0f : + sqrt(total.sum / (float)(total.num_pixels - 1)); + this->m_iscalculated = true; + } +} + +void CalculateStandardDeviationOperation::update_memory_buffer_partial( + MemoryBuffer *output, const rcti &area, Span<MemoryBuffer *> UNUSED(inputs)) +{ + output->fill(area, &m_standardDeviation); +} + +using PixelsSum = CalculateMeanOperation::PixelsSum; +PixelsSum CalculateStandardDeviationOperation::calc_area_sum(const MemoryBuffer *input, + const rcti &area, + const float mean) +{ + PixelsSum result = {0}; + for (const float *elem : input->get_buffer_area(area)) { + if (elem[3] <= 0.0f) { + continue; + } + const float value = setting_func_(elem); + result.sum += (value - mean) * (value - mean); + result.num_pixels++; + } + return result; +} + } // namespace blender::compositor diff --git a/source/blender/compositor/operations/COM_CalculateStandardDeviationOperation.h b/source/blender/compositor/operations/COM_CalculateStandardDeviationOperation.h index bc4aca69546..20de4cf4701 100644 --- a/source/blender/compositor/operations/COM_CalculateStandardDeviationOperation.h +++ b/source/blender/compositor/operations/COM_CalculateStandardDeviationOperation.h @@ -40,6 +40,17 @@ class CalculateStandardDeviationOperation : public CalculateMeanOperation { void executePixel(float output[4], int x, int y, void *data) override; void *initializeTileData(rcti *rect) override; + + void update_memory_buffer_started(MemoryBuffer *output, + const rcti &area, + Span<MemoryBuffer *> inputs) override; + + void update_memory_buffer_partial(MemoryBuffer *output, + const rcti &area, + Span<MemoryBuffer *> inputs) override; + + private: + PixelsSum calc_area_sum(const MemoryBuffer *input, const rcti &area, float mean); }; } // namespace blender::compositor |