From 6beaa297918b24de00d26886e981312b041748b0 Mon Sep 17 00:00:00 2001 From: Jeroen Bakker Date: Mon, 10 Jan 2022 08:57:53 +0100 Subject: Compositing Convert color space node Compositor node to convert between color spaces. Conversion is skipped when converting between the same color spaces or to or from data spaces. Implementation done for tiled and full frame compositor. Reviewed By: Blendify, jbakker Differential Revision: https://developer.blender.org/D12481 --- .../operations/COM_ConvertColorSpaceOperation.cc | 91 ++++++++++++++++++++++ .../operations/COM_ConvertColorSpaceOperation.h | 57 ++++++++++++++ 2 files changed, 148 insertions(+) create mode 100644 source/blender/compositor/operations/COM_ConvertColorSpaceOperation.cc create mode 100644 source/blender/compositor/operations/COM_ConvertColorSpaceOperation.h (limited to 'source/blender/compositor/operations') diff --git a/source/blender/compositor/operations/COM_ConvertColorSpaceOperation.cc b/source/blender/compositor/operations/COM_ConvertColorSpaceOperation.cc new file mode 100644 index 00000000000..5b1dfb4a02c --- /dev/null +++ b/source/blender/compositor/operations/COM_ConvertColorSpaceOperation.cc @@ -0,0 +1,91 @@ +/* + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * Copyright 2021, Blender Foundation. + */ + +#include "COM_ConvertColorSpaceOperation.h" + +namespace blender::compositor { + +ConvertColorSpaceOperation::ConvertColorSpaceOperation() +{ + this->add_input_socket(DataType::Color); + this->add_output_socket(DataType::Color); + this->input_program_ = nullptr; + color_processor_ = nullptr; +} + +void ConvertColorSpaceOperation::set_settings(NodeConvertColorSpace *node_color_space) +{ + this->settings_ = node_color_space; +} + +void ConvertColorSpaceOperation::init_execution() +{ + if (BLI_strnlen(settings_->from_color_space, sizeof(settings_->from_color_space)) == 0 || + BLI_strnlen(settings_->to_color_space, sizeof(settings_->to_color_space)) == 0) { + return; + } + + int in_colorspace_index = IMB_colormanagement_colorspace_get_named_index( + settings_->from_color_space); + int out_colorspace_index = IMB_colormanagement_colorspace_get_named_index( + settings_->to_color_space); + + if (in_colorspace_index == 0 || out_colorspace_index == 0) { + return; + } + + this->input_program_ = this->get_input_socket_reader(0); + + color_processor_ = IMB_colormanagement_colorspace_processor_new(settings_->from_color_space, + settings_->to_color_space); +} + +void ConvertColorSpaceOperation::execute_pixel_sampled(float output[4], + float x, + float y, + PixelSampler sampler) +{ + this->input_program_->read_sampled(output, x, y, sampler); + if (color_processor_ != nullptr) { + IMB_colormanagement_processor_apply_pixel(color_processor_, output, 3); + } +} + +void ConvertColorSpaceOperation::update_memory_buffer_partial(MemoryBuffer *output, + const rcti &area, + Span inputs) +{ + for (BuffersIterator it = output->iterate_with(inputs, area); !it.is_end(); ++it) { + copy_v4_v4(it.out, it.in(0)); + } + + if (color_processor_ != nullptr) { + output->apply_processor(*color_processor_, area); + } +} + +void ConvertColorSpaceOperation::deinit_execution() +{ + if (color_processor_ != nullptr) { + IMB_colormanagement_processor_free(color_processor_); + } + this->input_program_ = nullptr; + this->color_processor_ = nullptr; +} + +} // namespace blender::compositor diff --git a/source/blender/compositor/operations/COM_ConvertColorSpaceOperation.h b/source/blender/compositor/operations/COM_ConvertColorSpaceOperation.h new file mode 100644 index 00000000000..c065224eadc --- /dev/null +++ b/source/blender/compositor/operations/COM_ConvertColorSpaceOperation.h @@ -0,0 +1,57 @@ +/* + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * Copyright 2021, Blender Foundation. + */ + +#pragma once + +#include "COM_ConvertColorSpaceNode.h" +#include "COM_MultiThreadedOperation.h" +#include "IMB_colormanagement.h" + +namespace blender::compositor { + +class ConvertColorSpaceOperation : public MultiThreadedOperation { + private: + SocketReader *input_program_; + NodeConvertColorSpace *settings_; + ColormanageProcessor *color_processor_; + + public: + ConvertColorSpaceOperation(); + + void set_settings(NodeConvertColorSpace *node_color_space); + /** + * The inner loop of this operation. + */ + void execute_pixel_sampled(float output[4], float x, float y, PixelSampler sampler) override; + + /** + * Initialize the execution + */ + void init_execution() override; + + /** + * Deinitialize the execution + */ + void deinit_execution() override; + + void update_memory_buffer_partial(MemoryBuffer *output, + const rcti &area, + Span inputs) override; +}; + +} // namespace blender::compositor -- cgit v1.2.3