2022-02-11 09:07:11 +11:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
|
* Copyright 2012 Blender Foundation. */
|
2012-11-14 19:53:46 +00:00
|
|
|
|
|
|
|
|
#include "COM_MapRangeOperation.h"
|
|
|
|
|
|
2021-03-23 17:12:27 +01:00
|
|
|
namespace blender::compositor {
|
|
|
|
|
|
2020-08-07 15:58:58 +02:00
|
|
|
MapRangeOperation::MapRangeOperation()
|
2012-11-14 19:53:46 +00:00
|
|
|
{
|
2021-10-13 23:01:15 +02:00
|
|
|
this->add_input_socket(DataType::Value);
|
|
|
|
|
this->add_input_socket(DataType::Value);
|
|
|
|
|
this->add_input_socket(DataType::Value);
|
|
|
|
|
this->add_input_socket(DataType::Value);
|
|
|
|
|
this->add_input_socket(DataType::Value);
|
|
|
|
|
this->add_output_socket(DataType::Value);
|
|
|
|
|
input_operation_ = nullptr;
|
|
|
|
|
use_clamp_ = false;
|
2021-10-13 23:01:53 +02:00
|
|
|
flags_.can_be_constant = true;
|
2012-11-14 19:53:46 +00:00
|
|
|
}
|
|
|
|
|
|
2021-10-13 23:01:15 +02:00
|
|
|
void MapRangeOperation::init_execution()
|
2012-11-14 19:53:46 +00:00
|
|
|
{
|
2021-10-13 23:01:15 +02:00
|
|
|
input_operation_ = this->get_input_socket_reader(0);
|
|
|
|
|
source_min_operation_ = this->get_input_socket_reader(1);
|
|
|
|
|
source_max_operation_ = this->get_input_socket_reader(2);
|
|
|
|
|
dest_min_operation_ = this->get_input_socket_reader(3);
|
|
|
|
|
dest_max_operation_ = this->get_input_socket_reader(4);
|
2012-11-14 19:53:46 +00:00
|
|
|
}
|
|
|
|
|
|
2019-05-01 10:50:02 +10:00
|
|
|
/* The code below assumes all data is inside range +- this, and that input buffer is single channel
|
|
|
|
|
*/
|
2012-11-30 13:17:19 +00:00
|
|
|
#define BLENDER_ZMAX 10000.0f
|
|
|
|
|
|
2021-10-13 23:01:15 +02:00
|
|
|
void MapRangeOperation::execute_pixel_sampled(float output[4],
|
|
|
|
|
float x,
|
|
|
|
|
float y,
|
|
|
|
|
PixelSampler sampler)
|
2012-11-14 19:53:46 +00:00
|
|
|
{
|
|
|
|
|
float inputs[8]; /* includes the 5 inputs + 3 pads */
|
|
|
|
|
float value;
|
|
|
|
|
float source_min, source_max;
|
|
|
|
|
float dest_min, dest_max;
|
|
|
|
|
|
2021-10-13 23:01:15 +02:00
|
|
|
input_operation_->read_sampled(inputs, x, y, sampler);
|
|
|
|
|
source_min_operation_->read_sampled(inputs + 1, x, y, sampler);
|
|
|
|
|
source_max_operation_->read_sampled(inputs + 2, x, y, sampler);
|
|
|
|
|
dest_min_operation_->read_sampled(inputs + 3, x, y, sampler);
|
|
|
|
|
dest_max_operation_->read_sampled(inputs + 4, x, y, sampler);
|
2018-06-17 17:05:29 +02:00
|
|
|
|
2012-11-14 19:53:46 +00:00
|
|
|
value = inputs[0];
|
|
|
|
|
source_min = inputs[1];
|
|
|
|
|
source_max = inputs[2];
|
|
|
|
|
dest_min = inputs[3];
|
|
|
|
|
dest_max = inputs[4];
|
2012-11-30 13:17:19 +00:00
|
|
|
|
2018-03-05 11:44:42 +01:00
|
|
|
if (fabsf(source_max - source_min) < 1e-6f) {
|
|
|
|
|
output[0] = 0.0f;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-30 13:17:19 +00:00
|
|
|
if (value >= -BLENDER_ZMAX && value <= BLENDER_ZMAX) {
|
|
|
|
|
value = (value - source_min) / (source_max - source_min);
|
|
|
|
|
value = dest_min + value * (dest_max - dest_min);
|
|
|
|
|
}
|
2019-04-23 11:21:22 +10:00
|
|
|
else if (value > BLENDER_ZMAX) {
|
2012-11-30 13:17:19 +00:00
|
|
|
value = dest_max;
|
2019-04-23 11:21:22 +10:00
|
|
|
}
|
|
|
|
|
else {
|
2012-11-30 13:17:19 +00:00
|
|
|
value = dest_min;
|
2019-04-23 11:21:22 +10:00
|
|
|
}
|
2012-11-14 19:53:46 +00:00
|
|
|
|
2021-10-13 23:01:15 +02:00
|
|
|
if (use_clamp_) {
|
2012-11-14 22:15:45 +00:00
|
|
|
if (dest_max > dest_min) {
|
|
|
|
|
CLAMP(value, dest_min, dest_max);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
CLAMP(value, dest_max, dest_min);
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-11-14 19:53:46 +00:00
|
|
|
|
|
|
|
|
output[0] = value;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-13 23:01:15 +02:00
|
|
|
void MapRangeOperation::deinit_execution()
|
2012-11-14 19:53:46 +00:00
|
|
|
{
|
2021-10-13 23:01:15 +02:00
|
|
|
input_operation_ = nullptr;
|
|
|
|
|
source_min_operation_ = nullptr;
|
|
|
|
|
source_max_operation_ = nullptr;
|
|
|
|
|
dest_min_operation_ = nullptr;
|
|
|
|
|
dest_max_operation_ = nullptr;
|
2012-11-14 19:53:46 +00:00
|
|
|
}
|
2021-03-23 17:12:27 +01:00
|
|
|
|
2021-09-04 15:23:47 +02:00
|
|
|
void MapRangeOperation::update_memory_buffer_partial(MemoryBuffer *output,
|
|
|
|
|
const rcti &area,
|
|
|
|
|
Span<MemoryBuffer *> inputs)
|
|
|
|
|
{
|
|
|
|
|
for (BuffersIterator<float> it = output->iterate_with(inputs, area); !it.is_end(); ++it) {
|
|
|
|
|
const float source_min = *it.in(1);
|
|
|
|
|
const float source_max = *it.in(2);
|
|
|
|
|
if (fabsf(source_max - source_min) < 1e-6f) {
|
|
|
|
|
it.out[0] = 0.0f;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float value = *it.in(0);
|
|
|
|
|
const float dest_min = *it.in(3);
|
|
|
|
|
const float dest_max = *it.in(4);
|
|
|
|
|
if (value >= -BLENDER_ZMAX && value <= BLENDER_ZMAX) {
|
|
|
|
|
value = (value - source_min) / (source_max - source_min);
|
|
|
|
|
value = dest_min + value * (dest_max - dest_min);
|
|
|
|
|
}
|
|
|
|
|
else if (value > BLENDER_ZMAX) {
|
|
|
|
|
value = dest_max;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
value = dest_min;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-13 23:01:15 +02:00
|
|
|
if (use_clamp_) {
|
2021-09-04 15:23:47 +02:00
|
|
|
if (dest_max > dest_min) {
|
|
|
|
|
CLAMP(value, dest_min, dest_max);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
CLAMP(value, dest_max, dest_min);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
it.out[0] = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 17:12:27 +01:00
|
|
|
} // namespace blender::compositor
|