Integrate color temperature into RGB transformations

This commit is contained in:
LordGrey
2024-05-30 19:11:51 +02:00
parent 07dfce68f6
commit 5897e24316
17 changed files with 177 additions and 654 deletions

View File

@@ -598,16 +598,8 @@ void JsonAPI::handleAdjustmentCommand(const QJsonObject &message, const JsonApiC
return;
}
ColorCorrection *temperatureColorCorrection = _hyperion->getTemperature(adjustmentId);
if (temperatureColorCorrection == nullptr) {
Warning(_log, "Incorrect temperature adjustment identifier: %s", adjustmentId.toStdString().c_str());
return;
}
applyColorAdjustments(adjustment, colorAdjustment);
applyTransforms(adjustment, colorAdjustment);
applyTemperatureAdjustment(adjustment, temperatureColorCorrection);
_hyperion->adjustmentsUpdated();
sendSuccessReply(cmd);
}
@@ -644,6 +636,7 @@ void JsonAPI::applyTransforms(const QJsonObject &adjustment, ColorAdjustment *co
applyTransform("backlightColored", adjustment, colorAdjustment->_rgbTransform, &RgbTransform::setBacklightColored);
applyTransform("brightness", adjustment, colorAdjustment->_rgbTransform, &RgbTransform::setBrightness);
applyTransform("brightnessCompensation", adjustment, colorAdjustment->_rgbTransform, &RgbTransform::setBrightnessCompensation);
applyTransform("temperature", adjustment, colorAdjustment->_rgbTransform, &RgbTransform::setTemperature);
applyTransform("saturationGain", adjustment, colorAdjustment->_okhsvTransform, &OkhsvTransform::setSaturationGain);
applyTransform("brightnessGain", adjustment, colorAdjustment->_okhsvTransform, &OkhsvTransform::setBrightnessGain);
}
@@ -673,6 +666,14 @@ void JsonAPI::applyTransform(const QString &transformName, const QJsonObject &ad
}
}
template<typename T>
void JsonAPI::applyTransform(const QString &transformName, const QJsonObject &adjustment, T &transform, void (T::*setFunction)(int))
{
if (adjustment.contains(transformName)) {
(transform.*setFunction)(adjustment[transformName].toInt());
}
}
template<typename T>
void JsonAPI::applyTransform(const QString &transformName, const QJsonObject &adjustment, T &transform, void (T::*setFunction)(uint8_t))
{
@@ -681,19 +682,6 @@ void JsonAPI::applyTransform(const QString &transformName, const QJsonObject &ad
}
}
void JsonAPI::applyTemperatureAdjustment(const QJsonObject &adjustment, ColorCorrection *colorCorrection)
{
if (adjustment.contains("temperature"))
{
int temperature = adjustment["temperature"].toInt(6500);
ColorRgb rgb = getRgbFromTemperature(temperature);
colorCorrection->_rgbCorrection.setcorrectionR(rgb.red);
colorCorrection->_rgbCorrection.setcorrectionG(rgb.green);
colorCorrection->_rgbCorrection.setcorrectionB(rgb.blue);
}
}
void JsonAPI::handleSourceSelectCommand(const QJsonObject &message, const JsonApiCommand& cmd)
{
if (message.contains("auto"))

View File

@@ -83,15 +83,7 @@ QJsonArray JsonInfo::getAdjustmentInfo(const Hyperion* hyperion, Logger* log)
adjustment["saturationGain"] = colorAdjustment->_okhsvTransform.getSaturationGain();
adjustment["brightnessGain"] = colorAdjustment->_okhsvTransform.getBrightnessGain();
ColorCorrection *temperatureColorCorrection = hyperion->getTemperature(adjustmentId);
if (temperatureColorCorrection == nullptr) {
Error(log, "Incorrect temperature adjustment id: %s", QSTRING_CSTR(adjustmentId));
continue;
}
// TODO: Return current Temperature in Kelvin
adjustment["temperature"] = 6600;
adjustment["temperature"] = colorAdjustment->_rgbTransform.getTemperature();
adjustmentArray.append(adjustment);
}

View File

@@ -39,9 +39,6 @@ add_library(hyperion
# Led Color Transform
${CMAKE_SOURCE_DIR}/include/hyperion/MultiColorAdjustment.h
${CMAKE_SOURCE_DIR}/libsrc/hyperion/MultiColorAdjustment.cpp
${CMAKE_SOURCE_DIR}/include/hyperion/ColorCorrection.h
${CMAKE_SOURCE_DIR}/include/hyperion/MultiColorCorrection.h
${CMAKE_SOURCE_DIR}/libsrc/hyperion/MultiColorCorrection.cpp
# Priority Muxer
${CMAKE_SOURCE_DIR}/include/hyperion/PriorityMuxer.h
${CMAKE_SOURCE_DIR}/libsrc/hyperion/PriorityMuxer.cpp

View File

@@ -26,7 +26,6 @@
#include <leddevice/LedDeviceWrapper.h>
#include <hyperion/MultiColorAdjustment.h>
#include <hyperion/MultiColorCorrection.h>
#include <hyperion/LinearColorSmoothing.h>
#if defined(ENABLE_EFFECTENGINE)
@@ -57,7 +56,6 @@ Hyperion::Hyperion(quint8 instance, bool readonlyMode)
, _imageProcessor(nullptr)
, _muxer(nullptr)
, _raw2ledAdjustment(hyperion::createLedColorsAdjustment(static_cast<int>(_ledString.leds().size()), getSetting(settings::COLOR).object()))
, _raw2ledTemperature(hyperion::createLedColorsTemperature(static_cast<int>(_ledString.leds().size()), getSetting(settings::COLOR).object()))
, _ledDeviceWrapper(nullptr)
, _deviceSmooth(nullptr)
#if defined(ENABLE_EFFECTENGINE)
@@ -102,11 +100,6 @@ void Hyperion::start()
// get newVideoMode from HyperionIManager
connect(this, &Hyperion::newVideoMode, this, &Hyperion::handleNewVideoMode);
if (!_raw2ledTemperature->verifyCorrections())
{
Warning(_log, "Color temperature incorrectly set");
}
if (!_raw2ledAdjustment->verifyAdjustments())
{
Warning(_log, "At least one led has no color calibration, please add all leds from your led layout to an 'LED index' field!");
@@ -229,9 +222,6 @@ void Hyperion::freeObjects()
delete _raw2ledAdjustment;
// delete the color temperature correction
delete _raw2ledTemperature;
#if defined(ENABLE_FORWARDER)
delete _messageForwarder;
#endif
@@ -257,15 +247,6 @@ void Hyperion::handleSettingsUpdate(settings::type type, const QJsonDocument& co
{
Warning(_log, "At least one led has no color calibration, please add all leds from your led layout to an 'LED index' field!");
}
// change in color recreate ledTemperature
delete _raw2ledTemperature;
_raw2ledTemperature = hyperion::createLedColorsTemperature(static_cast<int>(_ledString.leds().size()), obj);
if (!_raw2ledTemperature->verifyCorrections())
{
Warning(_log, "At least one led has no color calibration, please add all leds from your led layout to an 'LED index' field!");
}
}
else if(type == settings::LEDS)
{
@@ -298,10 +279,6 @@ void Hyperion::handleSettingsUpdate(settings::type type, const QJsonDocument& co
delete _raw2ledAdjustment;
_raw2ledAdjustment = hyperion::createLedColorsAdjustment(static_cast<int>(_ledString.leds().size()), getSetting(settings::COLOR).object());
// change in leds recreate ledTemperature
delete _raw2ledTemperature;
_raw2ledTemperature = hyperion::createLedColorsTemperature(static_cast<int>(_ledString.leds().size()), getSetting(settings::COLOR).object());
#if defined(ENABLE_EFFECTENGINE)
// start cached effects
_effectEngine->startCachedEffects();
@@ -517,33 +494,17 @@ QStringList Hyperion::getAdjustmentIds() const
return _raw2ledAdjustment->getAdjustmentIds();
}
QStringList Hyperion::getTemperatureIds() const
{
return _raw2ledTemperature->getCorrectionIds();
}
ColorAdjustment * Hyperion::getAdjustment(const QString& id) const
{
return _raw2ledAdjustment->getAdjustment(id);
}
ColorCorrection * Hyperion::getTemperature(const QString& id) const
{
return _raw2ledTemperature->getCorrection(id);
}
void Hyperion::adjustmentsUpdated()
{
emit adjustmentChanged();
update();
}
void Hyperion::temperaturesUpdated()
{
emit adjustmentChanged();
update();
}
bool Hyperion::clear(int priority, bool forceClearAll)
{
bool isCleared = false;
@@ -736,7 +697,6 @@ void Hyperion::update()
emit rawLedColors(_ledBuffer);
_raw2ledAdjustment->applyAdjustment(_ledBuffer);
_raw2ledTemperature->applyCorrection(_ledBuffer);
int i = 0;
for (ColorRgb& color : _ledBuffer)

View File

@@ -1,22 +1,26 @@
#include <algorithm>
#include <limits>
// Hyperion includes
#include <utils/Logger.h>
#include <hyperion/MultiColorAdjustment.h>
constexpr uint32_t UINT8_MAX_SQUARED = static_cast<uint32_t>(std::numeric_limits<unsigned char>::max()) * static_cast<uint32_t>(std::numeric_limits<unsigned char>::max());
MultiColorAdjustment::MultiColorAdjustment(int ledCnt)
: _ledAdjustments(ledCnt, nullptr)
: _ledAdjustments(static_cast<size_t>(ledCnt), nullptr)
, _log(Logger::getInstance("ADJUSTMENT"))
{
}
MultiColorAdjustment::~MultiColorAdjustment()
{
// Clean up all the transforms
for (ColorAdjustment * adjustment : _adjustment)
for (ColorAdjustment* adjustment : _adjustment)
{
delete adjustment;
// BUG: Calling pop_back while iterating is invalid
_adjustment.pop_back();
}
_adjustment.clear();
}
void MultiColorAdjustment::addAdjustment(ColorAdjustment * adjustment)
@@ -25,7 +29,7 @@ void MultiColorAdjustment::addAdjustment(ColorAdjustment * adjustment)
_adjustment.push_back(adjustment);
}
void MultiColorAdjustment::setAdjustmentForLed(const QString& id, int startLed, int endLed)
void MultiColorAdjustment::setAdjustmentForLed(const QString& adjutmentId, int startLed, int endLed)
{
// abort
if(startLed > endLed)
@@ -41,8 +45,8 @@ void MultiColorAdjustment::setAdjustmentForLed(const QString& id, int startLed,
}
// Get the identified adjustment (don't care if is nullptr)
ColorAdjustment * adjustment = getAdjustment(id);
for (int iLed=startLed; iLed<=endLed; ++iLed)
ColorAdjustment * adjustment = getAdjustment(adjutmentId);
for (size_t iLed=static_cast<size_t>(startLed); iLed<=static_cast<size_t>(endLed); ++iLed)
{
_ledAdjustments[iLed] = adjustment;
}
@@ -50,18 +54,18 @@ void MultiColorAdjustment::setAdjustmentForLed(const QString& id, int startLed,
bool MultiColorAdjustment::verifyAdjustments() const
{
bool ok = true;
bool isAdjustmentDefined = true;
for (unsigned iLed=0; iLed<_ledAdjustments.size(); ++iLed)
{
ColorAdjustment * adjustment = _ledAdjustments[iLed];
const ColorAdjustment * adjustment = _ledAdjustments[iLed];
if (adjustment == nullptr)
{
Warning(_log, "No calibration set for led %d", iLed);
ok = false;
Warning(_log, "No calibration set for LED %d", iLed);
isAdjustmentDefined = false;
}
}
return ok;
return isAdjustmentDefined;
}
QStringList MultiColorAdjustment::getAdjustmentIds() const
@@ -69,15 +73,14 @@ QStringList MultiColorAdjustment::getAdjustmentIds() const
return _adjustmentIds;
}
ColorAdjustment* MultiColorAdjustment::getAdjustment(const QString& id)
ColorAdjustment* MultiColorAdjustment::getAdjustment(const QString& adjustmentId)
{
// Iterate through the unique adjustments until we find the one with the given id
for (ColorAdjustment* adjustment : _adjustment)
{
if (adjustment->_id == id)
{
return adjustment;
}
auto adjustmentIter = std::find_if(_adjustment.begin(), _adjustment.end(), [&adjustmentId](const ColorAdjustment* adjustment) {
return adjustment->_id == adjustmentId;
});
if (adjustmentIter != _adjustment.end()) {
return *adjustmentIter;
}
// The ColorAdjustment was not found
@@ -100,8 +103,7 @@ void MultiColorAdjustment::applyAdjustment(std::vector<ColorRgb>& ledColors)
ColorAdjustment* adjustment = _ledAdjustments[i];
if (adjustment == nullptr)
{
//std::cout << "MultiColorAdjustment::applyAdjustment() - No transform set for this led : " << i << std::endl;
// No transform set for this led (do nothing)
// No transform set for this LED (do nothing)
continue;
}
ColorRgb& color = ledColors[i];
@@ -117,27 +119,34 @@ void MultiColorAdjustment::applyAdjustment(std::vector<ColorRgb>& ledColors)
{
adjustment->_okhsvTransform.transform(ored, ogreen, oblue);
}
adjustment->_rgbTransform.transform(ored,ogreen,oblue);
adjustment->_rgbTransform.applyGamma(ored,ogreen,oblue);
adjustment->_rgbTransform.getBrightnessComponents(B_RGB, B_CMY, B_W);
uint32_t nrng = (uint32_t) (255-ored)*(255-ogreen);
uint32_t rng = (uint32_t) (ored) *(255-ogreen);
uint32_t nrg = (uint32_t) (255-ored)*(ogreen);
uint32_t rg = (uint32_t) (ored) *(ogreen);
uint32_t nr_ng = static_cast<uint32_t>((UINT8_MAX - ored) * (UINT8_MAX - ogreen));
uint32_t r_ng = static_cast<uint32_t>(ored * (UINT8_MAX - ogreen));
uint32_t nr_g = static_cast<uint32_t>((UINT8_MAX - ored) * ogreen);
uint32_t r_g = static_cast<uint32_t>(ored * ogreen);
uint8_t black = nrng*(255-oblue)/65025;
uint8_t red = rng *(255-oblue)/65025;
uint8_t green = nrg *(255-oblue)/65025;
uint8_t blue = nrng*(oblue) /65025;
uint8_t cyan = nrg *(oblue) /65025;
uint8_t magenta = rng *(oblue) /65025;
uint8_t yellow = rg *(255-oblue)/65025;
uint8_t white = rg *(oblue) /65025;
uint8_t black = static_cast<uint8_t>(nr_ng * (UINT8_MAX - oblue) / UINT8_MAX_SQUARED);
uint8_t red = static_cast<uint8_t>(r_ng * (UINT8_MAX - oblue) / UINT8_MAX_SQUARED);
uint8_t green = static_cast<uint8_t>(nr_g * (UINT8_MAX - oblue) / UINT8_MAX_SQUARED);
uint8_t blue = static_cast<uint8_t>(nr_ng * (oblue) / UINT8_MAX_SQUARED);
uint8_t cyan = static_cast<uint8_t>(nr_g * (oblue) / UINT8_MAX_SQUARED);
uint8_t magenta = static_cast<uint8_t>(r_ng * (oblue) / UINT8_MAX_SQUARED);
uint8_t yellow = static_cast<uint8_t>(r_g * (UINT8_MAX - oblue) / UINT8_MAX_SQUARED);
uint8_t white = static_cast<uint8_t>(r_g * (oblue) / UINT8_MAX_SQUARED);
uint8_t OR, OG, OB, RR, RG, RB, GR, GG, GB, BR, BG, BB;
uint8_t CR, CG, CB, MR, MG, MB, YR, YG, YB, WR, WG, WB;
uint8_t OR, OG, OB; // Original Colors
uint8_t RR, RG, RB; // Red Adjustments
uint8_t GR, GG, GB; // Green Adjustments
uint8_t BR, BG, BB; // Blue Adjustments
uint8_t CR, CG, CB; // Cyan Adjustments
uint8_t MR, MG, MB; // Magenta Adjustments
uint8_t YR, YG, YB; // Yellow Adjustments
uint8_t WR, WG, WB; // White Adjustments
adjustment->_rgbBlackAdjustment.apply (black , 255 , OR, OG, OB);
adjustment->_rgbBlackAdjustment.apply (black , UINT8_MAX, OR, OG, OB);
adjustment->_rgbRedAdjustment.apply (red , B_RGB, RR, RG, RB);
adjustment->_rgbGreenAdjustment.apply (green , B_RGB, GR, GG, GB);
adjustment->_rgbBlueAdjustment.apply (blue , B_RGB, BR, BG, BB);
@@ -149,5 +158,8 @@ void MultiColorAdjustment::applyAdjustment(std::vector<ColorRgb>& ledColors)
color.red = OR + RR + GR + BR + CR + MR + YR + WR;
color.green = OG + RG + GG + BG + CG + MG + YG + WG;
color.blue = OB + RB + GB + BB + CB + MB + YB + WB;
adjustment->_rgbTransform.applyTemperature(color);
adjustment->_rgbTransform.applyBacklight(color.red, color.green, color.green);
}
}

View File

@@ -1,103 +0,0 @@
// Hyperion includes
#include <utils/Logger.h>
#include <hyperion/MultiColorCorrection.h>
MultiColorCorrection::MultiColorCorrection(int ledCnt) :
_ledCorrections(ledCnt, nullptr)
, _log(Logger::getInstance("CORRECTION"))
{
}
MultiColorCorrection::~MultiColorCorrection()
{
// Clean up all the correctinos
for (ColorCorrection * correction : _correction)
{
delete correction;
}
}
void MultiColorCorrection::addCorrection(ColorCorrection * correction)
{
_correctionIds.push_back(correction->_id);
_correction.push_back(correction);
}
void MultiColorCorrection::setCorrectionForLed(const QString& id, int startLed, int endLed)
{
// abort
if(startLed > endLed)
{
Error(_log,"startLed > endLed -> %d > %d", startLed, endLed);
return;
}
// catch wrong values
if(endLed > static_cast<int>(_ledCorrections.size()-1))
{
Warning(_log,"The color correction 'LED index' field has LEDs specified which aren't part of your led layout");
endLed = static_cast<int>(_ledCorrections.size()-1);
}
// Get the identified correction (don't care if is nullptr)
ColorCorrection * correction = getCorrection(id);
for (int iLed=startLed; iLed<=endLed; ++iLed)
{
_ledCorrections[iLed] = correction;
}
}
bool MultiColorCorrection::verifyCorrections() const
{
bool ok = true;
for (unsigned iLed=0; iLed<_ledCorrections.size(); ++iLed)
{
ColorCorrection* adjustment = _ledCorrections[iLed];
if (adjustment == nullptr)
{
Warning(_log, "No correction set for led %d", iLed);
ok = false;
}
}
return ok;
}
QStringList & MultiColorCorrection::getCorrectionIds()
{
return _correctionIds;
}
ColorCorrection* MultiColorCorrection::getCorrection(const QString& id)
{
// Iterate through the unique corrections until we find the one with the given id
for (ColorCorrection * correction : _correction)
{
if (correction->_id == id)
{
return correction;
}
}
// The ColorCorrection was not found
return nullptr;
}
void MultiColorCorrection::applyCorrection(std::vector<ColorRgb>& ledColors)
{
const size_t itCnt = qMin(_ledCorrections.size(), ledColors.size());
for (size_t i=0; i<itCnt; ++i)
{
ColorCorrection * correction = _ledCorrections[i];
if (correction == nullptr)
{
std::cout << "MultiColorCorrection::applyCorrection() - No correction set for this led : " << i << std::endl;
// No correction set for this led (do nothing)
continue;
}
ColorRgb& color = ledColors[i];
color.red = correction->_rgbCorrection.correctionR(color.red);
color.green = correction->_rgbCorrection.correctionG(color.green);
color.blue = correction->_rgbCorrection.correctionB(color.blue);
}
}

View File

@@ -57,8 +57,6 @@ add_library(hyperion-utils
# Rgb single color adjustment/correction
${CMAKE_SOURCE_DIR}/include/utils/RgbChannelAdjustment.h
${CMAKE_SOURCE_DIR}/libsrc/utils/RgbChannelAdjustment.cpp
${CMAKE_SOURCE_DIR}/include/utils/RgbChannelCorrection.h
${CMAKE_SOURCE_DIR}/libsrc/utils/RgbChannelCorrection.cpp
# Color conversion/transformation
${CMAKE_SOURCE_DIR}/include/utils/RgbToRgbw.h
${CMAKE_SOURCE_DIR}/libsrc/utils/RgbToRgbw.cpp

View File

@@ -1,120 +0,0 @@
// STL includes
#include <cmath>
// Utils includes
#include <utils/RgbChannelCorrection.h>
RgbChannelCorrection::RgbChannelCorrection() :
_correctionR(255),
_correctionG(255),
_correctionB(255)
{
initializeMapping();
}
RgbChannelCorrection::RgbChannelCorrection(int correctionR, int correctionG, int correctionB) :
_correctionR(correctionR),
_correctionG(correctionG),
_correctionB(correctionB)
{
initializeMapping();
}
RgbChannelCorrection::~RgbChannelCorrection()
{
}
uint8_t RgbChannelCorrection::getcorrectionR() const
{
return _correctionR;
}
void RgbChannelCorrection::setcorrectionR(uint8_t correctionR)
{
_correctionR = correctionR;
initializeMapping();
}
uint8_t RgbChannelCorrection::getcorrectionG() const
{
return _correctionG;
}
void RgbChannelCorrection::setcorrectionG(uint8_t correctionG)
{
_correctionG = correctionG;
initializeMapping();
}
uint8_t RgbChannelCorrection::getcorrectionB() const
{
return _correctionB;
}
void RgbChannelCorrection::setcorrectionB(uint8_t correctionB)
{
_correctionB = correctionB;
initializeMapping();
}
uint8_t RgbChannelCorrection::correctionR(uint8_t inputR) const
{
return _mappingR[inputR];
}
uint8_t RgbChannelCorrection::correctionG(uint8_t inputG) const
{
return _mappingG[inputG];
}
uint8_t RgbChannelCorrection::correctionB(uint8_t inputB) const
{
return _mappingB[inputB];
}
void RgbChannelCorrection::initializeMapping()
{
// initialize the mapping
for (int i = 0; i < 256; ++i)
{
int outputR = (i * _correctionR) / 255;
if (outputR < -255)
{
outputR = -255;
}
else if (outputR > 255)
{
outputR = 255;
}
_mappingR[i] = outputR;
}
for (int i = 0; i < 256; ++i)
{
int outputG = (i * _correctionG) / 255;
if (outputG < -255)
{
outputG = -255;
}
else if (outputG > 255)
{
outputG = 255;
}
_mappingG[i] = outputG;
}
for (int i = 0; i < 256; ++i)
{
int outputB = (i * _correctionB) / 255;
if (outputB < -255)
{
outputB = -255;
}
else if (outputB > 255)
{
outputB = 255;
}
_mappingB[i] = outputB;
}
}

View File

@@ -1,19 +1,22 @@
#include <QtCore/qmath.h>
#include <utils/RgbTransform.h>
#include <utils/KelvinToRgb.h>
#include<QDebug>
RgbTransform::RgbTransform()
: RgbTransform::RgbTransform(1.0, 1.0, 1.0, 0.0, false, 100, 100)
: RgbTransform::RgbTransform(1.0, 1.0, 1.0, 0.0, false, 100, 100, 6600)
{
}
RgbTransform::RgbTransform(double gammaR, double gammaG, double gammaB, double backlightThreshold, bool backlightColored, uint8_t brightness, uint8_t brightnessCompensation)
RgbTransform::RgbTransform(double gammaR, double gammaG, double gammaB, double backlightThreshold, bool backlightColored, uint8_t brightness, uint8_t brightnessCompensation, int temperature)
: _brightness(brightness)
, _brightnessCompensation(brightnessCompensation)
{
init(gammaR, gammaG, gammaB, backlightThreshold, backlightColored, _brightness, _brightnessCompensation);
init(gammaR, gammaG, gammaB, backlightThreshold, backlightColored, _brightness, _brightnessCompensation, temperature);
}
void RgbTransform::init(double gammaR, double gammaG, double gammaB, double backlightThreshold, bool backlightColored, uint8_t brightness, uint8_t brightnessCompensation)
void RgbTransform::init(double gammaR, double gammaG, double gammaB, double backlightThreshold, bool backlightColored, uint8_t brightness, uint8_t brightnessCompensation, int temperature)
{
_backLightEnabled = true;
setGamma(gammaR,gammaG,gammaB);
@@ -21,6 +24,7 @@ void RgbTransform::init(double gammaR, double gammaG, double gammaB, double back
setBacklightColored(backlightColored);
setBrightness(brightness);
setBrightnessCompensation(brightnessCompensation);
setTemperature(temperature);
initializeMapping();
}
@@ -49,18 +53,34 @@ void RgbTransform::setGamma(double gammaR, double gammaG, double gammaB)
void RgbTransform::initializeMapping()
{
for (int i = 0; i < 256; ++i)
for (int i = 0; i <= UINT8_MAX; ++i)
{
_mappingR[i] = qMin(qMax((int)(qPow(i / 255.0, _gammaR) * 255), 0), 255);
_mappingG[i] = qMin(qMax((int)(qPow(i / 255.0, _gammaG) * 255), 0), 255);
_mappingB[i] = qMin(qMax((int)(qPow(i / 255.0, _gammaB) * 255), 0), 255);
// Calculate normalized value
double normalizedValueR = static_cast<double>(i) / UINT8_MAX;
double normalizedValueG = static_cast<double>(i) / UINT8_MAX;
double normalizedValueB = static_cast<double>(i) / UINT8_MAX;
// Apply gamma correction
double gammaCorrectedValueR = qPow(normalizedValueR, _gammaR) * UINT8_MAX;
double gammaCorrectedValueG = qPow(normalizedValueG, _gammaG) * UINT8_MAX;
double gammaCorrectedValueB = qPow(normalizedValueB, _gammaB) * UINT8_MAX;
// Clamp values to valid range [0, UINT8_MAX]
quint8 clampedValueR = static_cast<quint8>(qMin(qMax(gammaCorrectedValueR, 0.0), static_cast<double>(UINT8_MAX)));
quint8 clampedValueG = static_cast<quint8>(qMin(qMax(gammaCorrectedValueG, 0.0), static_cast<double>(UINT8_MAX)));
quint8 clampedValueB = static_cast<quint8>(qMin(qMax(gammaCorrectedValueB, 0.0), static_cast<double>(UINT8_MAX)));
// Assign clamped values to _mapping arrays
_mappingR[i] = clampedValueR;
_mappingG[i] = clampedValueG;
_mappingB[i] = clampedValueB;
}
}
int RgbTransform::getBacklightThreshold() const
{
return _backlightThreshold;
return static_cast<int>(_backlightThreshold);
}
void RgbTransform::setBacklightThreshold(double backlightThreshold)
@@ -116,60 +136,81 @@ void RgbTransform::updateBrightnessComponents()
double Fw = _brightnessCompensation*2.0/100.0+1.0;
double Fcmy = _brightnessCompensation/100.0+1.0;
double B_in= 0;
_brightness_rgb = 0;
_brightness_cmy = 0;
_brightness_w = 0;
if (_brightness > 0)
{
B_in = (_brightness<50)? -0.09*_brightness+7.5 : -0.04*_brightness+5.0;
double B_in = (_brightness < 50) ? -0.09 * _brightness + 7.5 : -0.04 * _brightness + 5.0;
_brightness_rgb = std::ceil(qMin(255.0,255.0/B_in));
_brightness_cmy = std::ceil(qMin(255.0,255.0/(B_in*Fcmy)));
_brightness_w = std::ceil(qMin(255.0,255.0/(B_in*Fw)));
// Ensure that the result is converted to an integer before assigning to uint8_t
_brightness_rgb = static_cast<uint8_t>(std::ceil(qMin(static_cast<double>(UINT8_MAX), UINT8_MAX / B_in)));
_brightness_cmy = static_cast<uint8_t>(std::ceil(qMin(static_cast<double>(UINT8_MAX), UINT8_MAX / (B_in * Fcmy))));
_brightness_w = static_cast<uint8_t>(std::ceil(qMin(static_cast<double>(UINT8_MAX), UINT8_MAX / (B_in * Fw))));
}
}
void RgbTransform::getBrightnessComponents(uint8_t & rgb, uint8_t & cmy, uint8_t & w) const
void RgbTransform::getBrightnessComponents(uint8_t & rgb, uint8_t & cmy, uint8_t & white) const
{
rgb = _brightness_rgb;
cmy = _brightness_cmy;
w = _brightness_w;
white = _brightness_w;
}
void RgbTransform::transform(uint8_t & red, uint8_t & green, uint8_t & blue)
void RgbTransform::applyGamma(uint8_t & red, uint8_t & green, uint8_t & blue)
{
// apply gamma
red = _mappingR[red];
green = _mappingG[green];
blue = _mappingB[blue];
}
void RgbTransform::applyBacklight(uint8_t & red, uint8_t & green, uint8_t & blue) const
{
// apply brightnesss
int rgbSum = red+green+blue;
if ( _backLightEnabled && _sumBrightnessLow>0 && rgbSum < _sumBrightnessLow)
if ( _backLightEnabled && _sumBrightnessLow > 0 && rgbSum < _sumBrightnessLow)
{
if (_backlightColored)
{
if (rgbSum == 0)
{
if (red ==0) red = 1;
if (green==0) green = 1;
if (blue ==0) blue = 1;
if (red ==0) { red = 1; }
if (green==0) { green = 1; }
if (blue ==0) { blue = 1; }
rgbSum = red+green+blue;
}
double cL =qMin((int)(_sumBrightnessLow /rgbSum), 255);
red *= cL;
green *= cL;
blue *= cL;
uint8_t cLow = static_cast<uint8_t>(qMin(static_cast<int>(_sumBrightnessLow / rgbSum), UINT8_MAX));
red *= cLow;
green *= cLow;
blue *= cLow;
}
else
{
red = qMin((int)(_sumBrightnessLow/3.0), 255);
red = static_cast<uint8_t>(qMin(static_cast<int>(_sumBrightnessLow/3.0), UINT8_MAX));
green = red;
blue = red;
}
}
}
void RgbTransform::setTemperature(int temperature)
{
_temperature = temperature;
_temperatureRGB = getRgbFromTemperature(_temperature);
}
int RgbTransform::getTemperature() const
{
return _temperature;
}
void RgbTransform::applyTemperature(ColorRgb& color) const
{
color.red = color.red * _temperatureRGB.red / UINT8_MAX;
color.green = color.green * _temperatureRGB.green / UINT8_MAX;
color.blue = color.blue * _temperatureRGB.blue / UINT8_MAX;
}