This repository has been archived on 2023-10-09. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
blender-archive/source/blender/compositor/operations/COM_MapRangeOperation.cc
Manuel Castilla d84c79a218 Compositor: Full frame vector nodes
Adds full frame implementation to Map Range, Map Value, Normal and
Normalize nodes. The other nodes in "Vector" sub-menu are submitted
separately.

Part of T88150.

Reviewed By: jbakker

Differential Revision: https://developer.blender.org/D12233
2021-09-04 17:06:01 +02:00

148 lines
4.4 KiB
C++

/*
* 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 2012, Blender Foundation.
*/
#include "COM_MapRangeOperation.h"
namespace blender::compositor {
MapRangeOperation::MapRangeOperation()
{
this->addInputSocket(DataType::Value);
this->addInputSocket(DataType::Value);
this->addInputSocket(DataType::Value);
this->addInputSocket(DataType::Value);
this->addInputSocket(DataType::Value);
this->addOutputSocket(DataType::Value);
this->m_inputOperation = nullptr;
this->m_useClamp = false;
flags.can_be_constant = true;
}
void MapRangeOperation::initExecution()
{
this->m_inputOperation = this->getInputSocketReader(0);
this->m_sourceMinOperation = this->getInputSocketReader(1);
this->m_sourceMaxOperation = this->getInputSocketReader(2);
this->m_destMinOperation = this->getInputSocketReader(3);
this->m_destMaxOperation = this->getInputSocketReader(4);
}
/* The code below assumes all data is inside range +- this, and that input buffer is single channel
*/
#define BLENDER_ZMAX 10000.0f
void MapRangeOperation::executePixelSampled(float output[4],
float x,
float y,
PixelSampler sampler)
{
float inputs[8]; /* includes the 5 inputs + 3 pads */
float value;
float source_min, source_max;
float dest_min, dest_max;
this->m_inputOperation->readSampled(inputs, x, y, sampler);
this->m_sourceMinOperation->readSampled(inputs + 1, x, y, sampler);
this->m_sourceMaxOperation->readSampled(inputs + 2, x, y, sampler);
this->m_destMinOperation->readSampled(inputs + 3, x, y, sampler);
this->m_destMaxOperation->readSampled(inputs + 4, x, y, sampler);
value = inputs[0];
source_min = inputs[1];
source_max = inputs[2];
dest_min = inputs[3];
dest_max = inputs[4];
if (fabsf(source_max - source_min) < 1e-6f) {
output[0] = 0.0f;
return;
}
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;
}
if (this->m_useClamp) {
if (dest_max > dest_min) {
CLAMP(value, dest_min, dest_max);
}
else {
CLAMP(value, dest_max, dest_min);
}
}
output[0] = value;
}
void MapRangeOperation::deinitExecution()
{
this->m_inputOperation = nullptr;
this->m_sourceMinOperation = nullptr;
this->m_sourceMaxOperation = nullptr;
this->m_destMinOperation = nullptr;
this->m_destMaxOperation = nullptr;
}
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;
}
if (m_useClamp) {
if (dest_max > dest_min) {
CLAMP(value, dest_min, dest_max);
}
else {
CLAMP(value, dest_max, dest_min);
}
}
it.out[0] = value;
}
}
} // namespace blender::compositor