2013-08-13 11:10:45 +02:00
|
|
|
// STL includes
|
2013-08-05 23:49:17 +02:00
|
|
|
#include <cmath>
|
|
|
|
|
|
|
|
#include "ColorTransform.h"
|
|
|
|
|
2013-08-13 11:10:45 +02:00
|
|
|
using namespace hyperion;
|
|
|
|
|
2013-08-05 23:49:17 +02:00
|
|
|
ColorTransform::ColorTransform() :
|
2013-08-13 11:10:45 +02:00
|
|
|
_threshold(0),
|
|
|
|
_gamma(1.0),
|
|
|
|
_blacklevel(0.0),
|
|
|
|
_whitelevel(1.0)
|
2013-08-05 23:49:17 +02:00
|
|
|
{
|
2013-08-13 11:10:45 +02:00
|
|
|
initializeMapping();
|
2013-08-05 23:49:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ColorTransform::ColorTransform(double threshold, double gamma, double blacklevel, double whitelevel) :
|
2013-08-13 11:10:45 +02:00
|
|
|
_threshold(threshold),
|
|
|
|
_gamma(gamma),
|
|
|
|
_blacklevel(blacklevel),
|
|
|
|
_whitelevel(whitelevel)
|
2013-08-05 23:49:17 +02:00
|
|
|
{
|
2013-08-13 11:10:45 +02:00
|
|
|
initializeMapping();
|
2013-08-05 23:49:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ColorTransform::~ColorTransform()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
double ColorTransform::getThreshold() const
|
|
|
|
{
|
2013-08-13 11:10:45 +02:00
|
|
|
return _threshold;
|
2013-08-05 23:49:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ColorTransform::setThreshold(double threshold)
|
|
|
|
{
|
2013-08-13 11:10:45 +02:00
|
|
|
_threshold = threshold;
|
|
|
|
initializeMapping();
|
2013-08-05 23:49:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
double ColorTransform::getGamma() const
|
|
|
|
{
|
2013-08-13 11:10:45 +02:00
|
|
|
return _gamma;
|
2013-08-05 23:49:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ColorTransform::setGamma(double gamma)
|
|
|
|
{
|
2013-08-13 11:10:45 +02:00
|
|
|
_gamma = gamma;
|
|
|
|
initializeMapping();
|
2013-08-05 23:49:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
double ColorTransform::getBlacklevel() const
|
|
|
|
{
|
2013-08-13 11:10:45 +02:00
|
|
|
return _blacklevel;
|
2013-08-05 23:49:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ColorTransform::setBlacklevel(double blacklevel)
|
|
|
|
{
|
2013-08-13 11:10:45 +02:00
|
|
|
_blacklevel = blacklevel;
|
|
|
|
initializeMapping();
|
2013-08-05 23:49:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
double ColorTransform::getWhitelevel() const
|
|
|
|
{
|
2013-08-13 11:10:45 +02:00
|
|
|
return _whitelevel;
|
2013-08-05 23:49:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ColorTransform::setWhitelevel(double whitelevel)
|
|
|
|
{
|
2013-08-13 11:10:45 +02:00
|
|
|
_whitelevel = whitelevel;
|
|
|
|
initializeMapping();
|
2013-08-05 23:49:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ColorTransform::initializeMapping()
|
|
|
|
{
|
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
|
|
|
}
|