hyperion.ng/libsrc/utils/RgbChannelTransform.cpp

105 lines
1.8 KiB
C++
Raw Normal View History

// STL includes
2013-08-05 23:49:17 +02:00
#include <cmath>
// Utils includes
#include <utils/RgbChannelTransform.h>
RgbChannelTransform::RgbChannelTransform() :
_threshold(0),
_gamma(1.0),
_blacklevel(0.0),
_whitelevel(1.0)
2013-08-05 23:49:17 +02:00
{
initializeMapping();
2013-08-05 23:49:17 +02:00
}
RgbChannelTransform::RgbChannelTransform(double threshold, double gamma, double blacklevel, double whitelevel) :
_threshold(threshold),
_gamma(gamma),
_blacklevel(blacklevel),
_whitelevel(whitelevel)
2013-08-05 23:49:17 +02:00
{
initializeMapping();
2013-08-05 23:49:17 +02:00
}
RgbChannelTransform::~RgbChannelTransform()
2013-08-05 23:49:17 +02:00
{
}
double RgbChannelTransform::getThreshold() const
2013-08-05 23:49:17 +02:00
{
return _threshold;
2013-08-05 23:49:17 +02:00
}
void RgbChannelTransform::setThreshold(double threshold)
2013-08-05 23:49:17 +02:00
{
_threshold = threshold;
initializeMapping();
2013-08-05 23:49:17 +02:00
}
double RgbChannelTransform::getGamma() const
2013-08-05 23:49:17 +02:00
{
return _gamma;
2013-08-05 23:49:17 +02:00
}
void RgbChannelTransform::setGamma(double gamma)
2013-08-05 23:49:17 +02:00
{
_gamma = gamma;
initializeMapping();
2013-08-05 23:49:17 +02:00
}
double RgbChannelTransform::getBlacklevel() const
2013-08-05 23:49:17 +02:00
{
return _blacklevel;
2013-08-05 23:49:17 +02:00
}
void RgbChannelTransform::setBlacklevel(double blacklevel)
2013-08-05 23:49:17 +02:00
{
_blacklevel = blacklevel;
initializeMapping();
2013-08-05 23:49:17 +02:00
}
double RgbChannelTransform::getWhitelevel() const
2013-08-05 23:49:17 +02:00
{
return _whitelevel;
2013-08-05 23:49:17 +02:00
}
void RgbChannelTransform::setWhitelevel(double whitelevel)
2013-08-05 23:49:17 +02:00
{
_whitelevel = whitelevel;
initializeMapping();
2013-08-05 23:49:17 +02:00
}
void RgbChannelTransform::initializeMapping()
2013-08-05 23:49:17 +02:00
{
// initialize the mapping as a linear array
for (int i = 0; i < 256; ++i)
{
double output = i / 255.0;
// apply linear transform
if (output < _threshold)
{
output = 0.0;
}
// apply gamma correction
output = std::pow(output, _gamma);
// apply blacklevel and whitelevel
output = _blacklevel + (_whitelevel - _blacklevel) * output;
// calc mapping
int mappingValue = output * 255;
if (mappingValue < 0)
{
mappingValue = 0;
}
else if (mappingValue > 255)
{
mappingValue = 255;
}
_mapping[i] = mappingValue;
}
2013-08-05 23:49:17 +02:00
}