2013-08-13 11:10:45 +02:00
|
|
|
// STL includes
|
2013-08-05 23:49:17 +02:00
|
|
|
#include <cmath>
|
|
|
|
|
2013-11-19 21:17:59 +01:00
|
|
|
// Utils includes
|
|
|
|
#include <utils/RgbChannelTransform.h>
|
2013-08-13 11:10:45 +02:00
|
|
|
|
2016-07-04 00:43:41 +02:00
|
|
|
RgbChannelTransform::RgbChannelTransform()
|
2013-08-05 23:49:17 +02:00
|
|
|
{
|
2016-07-04 00:43:41 +02:00
|
|
|
setTransform(0.0, 1.0, 0.0, 1.0);
|
2013-08-05 23:49:17 +02:00
|
|
|
}
|
|
|
|
|
2016-07-04 00:43:41 +02:00
|
|
|
RgbChannelTransform::RgbChannelTransform(double threshold, double gamma, double blacklevel, double whitelevel)
|
2013-08-05 23:49:17 +02:00
|
|
|
{
|
2016-07-04 00:43:41 +02:00
|
|
|
setTransform(threshold, gamma, blacklevel, whitelevel);
|
2013-08-05 23:49:17 +02:00
|
|
|
}
|
|
|
|
|
2013-11-19 21:17:59 +01:00
|
|
|
RgbChannelTransform::~RgbChannelTransform()
|
2013-08-05 23:49:17 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-07-04 00:43:41 +02:00
|
|
|
void RgbChannelTransform::setTransform(double threshold, double gamma, double blacklevel, double whitelevel)
|
|
|
|
{
|
|
|
|
_threshold = threshold;
|
|
|
|
_gamma = gamma;
|
|
|
|
_blacklevel = blacklevel;
|
|
|
|
_whitelevel = whitelevel;
|
|
|
|
initializeMapping();
|
|
|
|
}
|
|
|
|
|
2013-11-19 21:17:59 +01:00
|
|
|
double RgbChannelTransform::getThreshold() const
|
2013-08-05 23:49:17 +02:00
|
|
|
{
|
2013-08-13 11:10:45 +02:00
|
|
|
return _threshold;
|
2013-08-05 23:49:17 +02:00
|
|
|
}
|
|
|
|
|
2013-11-19 21:17:59 +01:00
|
|
|
void RgbChannelTransform::setThreshold(double threshold)
|
2013-08-05 23:49:17 +02:00
|
|
|
{
|
2016-07-04 00:43:41 +02:00
|
|
|
setTransform(threshold, _gamma, _blacklevel, _whitelevel);
|
2013-08-05 23:49:17 +02:00
|
|
|
}
|
|
|
|
|
2013-11-19 21:17:59 +01:00
|
|
|
double RgbChannelTransform::getGamma() const
|
2013-08-05 23:49:17 +02:00
|
|
|
{
|
2013-08-13 11:10:45 +02:00
|
|
|
return _gamma;
|
2013-08-05 23:49:17 +02:00
|
|
|
}
|
|
|
|
|
2013-11-19 21:17:59 +01:00
|
|
|
void RgbChannelTransform::setGamma(double gamma)
|
2013-08-05 23:49:17 +02:00
|
|
|
{
|
2016-07-04 00:43:41 +02:00
|
|
|
setTransform(_threshold, gamma, _blacklevel, _whitelevel);
|
2013-08-05 23:49:17 +02:00
|
|
|
}
|
|
|
|
|
2013-11-19 21:17:59 +01:00
|
|
|
double RgbChannelTransform::getBlacklevel() const
|
2013-08-05 23:49:17 +02:00
|
|
|
{
|
2013-08-13 11:10:45 +02:00
|
|
|
return _blacklevel;
|
2013-08-05 23:49:17 +02:00
|
|
|
}
|
|
|
|
|
2013-11-19 21:17:59 +01:00
|
|
|
void RgbChannelTransform::setBlacklevel(double blacklevel)
|
2013-08-05 23:49:17 +02:00
|
|
|
{
|
2016-07-04 00:43:41 +02:00
|
|
|
setTransform(_threshold, _gamma, blacklevel, _whitelevel);
|
2013-08-05 23:49:17 +02:00
|
|
|
}
|
|
|
|
|
2013-11-19 21:17:59 +01:00
|
|
|
double RgbChannelTransform::getWhitelevel() const
|
2013-08-05 23:49:17 +02:00
|
|
|
{
|
2013-08-13 11:10:45 +02:00
|
|
|
return _whitelevel;
|
2013-08-05 23:49:17 +02:00
|
|
|
}
|
|
|
|
|
2013-11-19 21:17:59 +01:00
|
|
|
void RgbChannelTransform::setWhitelevel(double whitelevel)
|
2013-08-05 23:49:17 +02:00
|
|
|
{
|
2016-07-04 00:43:41 +02:00
|
|
|
setTransform(_threshold, _gamma, _blacklevel, whitelevel);
|
2013-08-05 23:49:17 +02:00
|
|
|
}
|
|
|
|
|
2013-11-19 21:17:59 +01:00
|
|
|
void RgbChannelTransform::initializeMapping()
|
2013-08-05 23:49:17 +02:00
|
|
|
{
|
2013-08-13 11:10:45 +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
|
|
|
}
|