Adjustment merge + new brightness settings (#359)

* add new rgbtransform

* activate rgbtransform

* integrate new transform and gamma in adjustment, disable transform

* fix brighness limit

* advance upper and lower thresholds

* start removing color transform

* adjust configs/schema

* implement json for new color adjustment

* finish hyperion-remote extension for new adjustment settings

* fix typos

* rename luminance to brightness
fix jsonapi for new adjustment

* fix some bugs in adjustments

* fix i18n

* fix gamma via json

* now brighness values goes from 0-1 with 0.5 is the default for all brighness is equal between the channels. less 0.5 all channels scaled down
to new brighness, above 0.5 if possible channel gets brighter - but brighness is not equal between the channels anymore
brighness value curve is now exponential instead of linear - this feels more natural

* hslv cleanup
This commit is contained in:
redPanther
2017-01-06 14:25:55 +01:00
committed by GitHub
parent c433504b81
commit caab8e819b
34 changed files with 645 additions and 1807 deletions

View File

@@ -19,7 +19,6 @@ SET(Hyperion_HEADERS
${CURRENT_HEADER_DIR}/LedString.h
${CURRENT_HEADER_DIR}/PriorityMuxer.h
${CURRENT_SOURCE_DIR}/MultiColorTransform.h
${CURRENT_SOURCE_DIR}/MultiColorAdjustment.h
${CURRENT_HEADER_DIR}/MessageForwarder.h
)
@@ -32,7 +31,6 @@ SET(Hyperion_SOURCES
${CURRENT_SOURCE_DIR}/PriorityMuxer.cpp
${CURRENT_SOURCE_DIR}/ImageToLedsMap.cpp
${CURRENT_SOURCE_DIR}/MultiColorTransform.cpp
${CURRENT_SOURCE_DIR}/MultiColorAdjustment.cpp
${CURRENT_SOURCE_DIR}/LinearColorSmoothing.cpp
${CURRENT_SOURCE_DIR}/MessageForwarder.cpp

View File

@@ -18,14 +18,12 @@
#include <hyperion/Hyperion.h>
#include <hyperion/ImageProcessorFactory.h>
#include <hyperion/ImageProcessor.h>
#include <hyperion/ColorTransform.h>
#include <hyperion/ColorAdjustment.h>
// Leddevice includes
#include <leddevice/LedDevice.h>
#include <leddevice/LedDeviceFactory.h>
#include "MultiColorTransform.h"
#include "MultiColorAdjustment.h"
#include "LinearColorSmoothing.h"
@@ -56,37 +54,6 @@ ColorOrder Hyperion::createColorOrder(const QJsonObject &deviceConfig)
return stringToColorOrder(deviceConfig["colorOrder"].toString("rgb"));
}
ColorTransform * Hyperion::createColorTransform(const QJsonObject & transformConfig)
{
const std::string id = transformConfig["id"].toString("default").toStdString();
RgbChannelTransform * redTransform = createRgbChannelTransform(transformConfig["red"].toObject());
RgbChannelTransform * greenTransform = createRgbChannelTransform(transformConfig["green"].toObject());
RgbChannelTransform * blueTransform = createRgbChannelTransform(transformConfig["blue"].toObject());
HsvTransform * hsvTransform = createHsvTransform(transformConfig["hsv"].toObject());
HslTransform * hslTransform = createHslTransform(transformConfig["hsl"].toObject());
ColorTransform * transform = new ColorTransform();
transform->_id = id;
transform->_rgbRedTransform = *redTransform;
transform->_rgbGreenTransform = *greenTransform;
transform->_rgbBlueTransform = *blueTransform;
transform->_hsvTransform = *hsvTransform;
transform->_hslTransform = *hslTransform;
// Cleanup the allocated individual transforms
delete redTransform;
delete greenTransform;
delete blueTransform;
delete hsvTransform;
delete hslTransform;
return transform;
}
ColorAdjustment * Hyperion::createColorAdjustment(const QJsonObject & adjustmentConfig)
{
const std::string id = adjustmentConfig["id"].toString("default").toStdString();
@@ -109,7 +76,8 @@ ColorAdjustment * Hyperion::createColorAdjustment(const QJsonObject & adjustment
RgbChannelAdjustment * cyanAdjustment = createRgbChannelAdjustment(adjustmentConfig["cyan"].toArray(),CYAN);
RgbChannelAdjustment * magentaAdjustment = createRgbChannelAdjustment(adjustmentConfig["magenta"].toArray(),MAGENTA);
RgbChannelAdjustment * yellowAdjustment = createRgbChannelAdjustment(adjustmentConfig["yellow"].toArray(),YELLOW);
RgbTransform * rgbTransform = createRgbTransform(adjustmentConfig);
ColorAdjustment * adjustment = new ColorAdjustment();
adjustment->_id = id;
adjustment->_rgbBlackAdjustment = *blackAdjustment;
@@ -120,6 +88,7 @@ ColorAdjustment * Hyperion::createColorAdjustment(const QJsonObject & adjustment
adjustment->_rgbCyanAdjustment = *cyanAdjustment;
adjustment->_rgbMagentaAdjustment = *magentaAdjustment;
adjustment->_rgbYellowAdjustment = *yellowAdjustment;
adjustment->_rgbTransform = *rgbTransform;
// Cleanup the allocated individual adjustments
delete blackAdjustment;
@@ -130,86 +99,12 @@ ColorAdjustment * Hyperion::createColorAdjustment(const QJsonObject & adjustment
delete cyanAdjustment;
delete magentaAdjustment;
delete yellowAdjustment;
delete rgbTransform;
return adjustment;
}
MultiColorTransform * Hyperion::createLedColorsTransform(const unsigned ledCnt, const QJsonObject & colorConfig)
{
// Create the result, the transforms are added to this
MultiColorTransform * transform = new MultiColorTransform(ledCnt);
Logger * log = Logger::getInstance("Core");
const QJsonValue transformConfig = colorConfig["transform"];
if (transformConfig.isNull())
{
// Old style color transformation config (just one for all leds)
ColorTransform * colorTransform = createColorTransform(colorConfig);
transform->addTransform(colorTransform);
transform->setTransformForLed(colorTransform->_id, 0, ledCnt-1);
}
else if (transformConfig.isObject())
{
ColorTransform * colorTransform = createColorTransform(transformConfig.toObject());
transform->addTransform(colorTransform);
transform->setTransformForLed(colorTransform->_id, 0, ledCnt-1);
}
else if (transformConfig.isArray())
{
const QRegExp overallExp("([0-9]+(\\-[0-9]+)?)(,[ ]*([0-9]+(\\-[0-9]+)?))*");
const QJsonArray & transformConfigArray = transformConfig.toArray();
for (signed i = 0; i < transformConfigArray.size(); ++i)
{
const QJsonObject & config = transformConfigArray[i].toObject();
ColorTransform * colorTransform = createColorTransform(config);
transform->addTransform(colorTransform);
const QString ledIndicesStr = config["leds"].toString("").trimmed();
if (ledIndicesStr.compare("*") == 0)
{
// Special case for indices '*' => all leds
transform->setTransformForLed(colorTransform->_id, 0, ledCnt-1);
Info(log, "ColorTransform '%s' => [0; %d]", colorTransform->_id.c_str(), ledCnt-1);
continue;
}
if (!overallExp.exactMatch(ledIndicesStr))
{
Error(log, "Given led indices %d not correct format: %s", i, ledIndicesStr.toStdString().c_str());
continue;
}
std::stringstream ss;
const QStringList ledIndexList = ledIndicesStr.split(",");
for (int i=0; i<ledIndexList.size(); ++i) {
if (i > 0)
{
ss << ", ";
}
if (ledIndexList[i].contains("-"))
{
QStringList ledIndices = ledIndexList[i].split("-");
int startInd = ledIndices[0].toInt();
int endInd = ledIndices[1].toInt();
transform->setTransformForLed(colorTransform->_id, startInd, endInd);
ss << startInd << "-" << endInd;
}
else
{
int index = ledIndexList[i].toInt();
transform->setTransformForLed(colorTransform->_id, index, index);
ss << index;
}
}
Info(log, "ColorTransform '%s' => [%s]", colorTransform->_id.c_str(), ss.str().c_str());
}
}
return transform;
}
MultiColorAdjustment * Hyperion::createLedColorsAdjustment(const unsigned ledCnt, const QJsonObject & colorConfig)
{
// Create the result, the transforms are added to this
@@ -285,31 +180,15 @@ MultiColorAdjustment * Hyperion::createLedColorsAdjustment(const unsigned ledCnt
return adjustment;
}
HsvTransform * Hyperion::createHsvTransform(const QJsonObject & hsvConfig)
RgbTransform* Hyperion::createRgbTransform(const QJsonObject& colorConfig)
{
const double saturationGain = hsvConfig["saturationGain"].toDouble(1.0);
const double valueGain = hsvConfig["valueGain"].toDouble(1.0);
const double brightnessMin = colorConfig["brightnessMin"].toDouble(0.0);
const double brightness = colorConfig["brightness"].toDouble(0.5);
const double gammaR = colorConfig["gammaRed"].toDouble(1.0);
const double gammaG = colorConfig["gammaGreen"].toDouble(1.0);
const double gammaB = colorConfig["gammaBlue"].toDouble(1.0);
return new HsvTransform(saturationGain, valueGain);
}
HslTransform * Hyperion::createHslTransform(const QJsonObject & hslConfig)
{
const double saturationGain = hslConfig["saturationGain"].toDouble(1.0);
const double luminanceGain = hslConfig["luminanceGain"].toDouble(1.0);
const double luminanceMinimum = hslConfig["luminanceMinimum"].toDouble(0.0);
return new HslTransform(saturationGain, luminanceGain, luminanceMinimum);
}
RgbChannelTransform* Hyperion::createRgbChannelTransform(const QJsonObject& colorConfig)
{
const double threshold = colorConfig["threshold"].toDouble(0.0);
const double gamma = colorConfig["gamma"].toDouble(1.0);
const double blacklevel = colorConfig["blacklevel"].toDouble(0.0);
const double whitelevel = colorConfig["whitelevel"].toDouble(1.0);
RgbChannelTransform* transform = new RgbChannelTransform(threshold, gamma, blacklevel, whitelevel);
RgbTransform* transform = new RgbTransform(gammaR, gammaG, gammaB, brightnessMin, brightness);
return transform;
}
@@ -575,7 +454,6 @@ Hyperion::Hyperion(const QJsonObject &qjsonConfig, const QString configFile)
: _ledString(createLedString(qjsonConfig["leds"], createColorOrder(qjsonConfig["device"].toObject())))
, _ledStringClone(createLedStringClone(qjsonConfig["leds"], createColorOrder(qjsonConfig["device"].toObject())))
, _muxer(_ledString.leds().size())
, _raw2ledTransform(createLedColorsTransform(_ledString.leds().size(), qjsonConfig["color"].toObject()))
, _raw2ledAdjustment(createLedColorsAdjustment(_ledString.leds().size(), qjsonConfig["color"].toObject()))
, _effectEngine(nullptr)
, _messageForwarder(createMessageForwarder(qjsonConfig["forwarder"].toObject()))
@@ -585,33 +463,22 @@ Hyperion::Hyperion(const QJsonObject &qjsonConfig, const QString configFile)
, _log(Logger::getInstance("Core"))
, _hwLedCount(_ledString.leds().size())
, _colorAdjustmentV4Lonly(false)
, _colorTransformV4Lonly(false)
, _sourceAutoSelectEnabled(true)
, _configHash()
, _ledGridSize(getLedLayoutGridSize(qjsonConfig["leds"]))
{
registerPriority("Off", PriorityMuxer::LOWEST_PRIORITY);
if (!_raw2ledAdjustment->verifyAdjustments())
{
throw std::runtime_error("Color adjustment incorrectly set");
}
if (!_raw2ledTransform->verifyTransforms())
{
throw std::runtime_error("Color transformation incorrectly set");
}
// set color correction activity state
const QJsonObject& color = qjsonConfig["color"].toObject();
_transformEnabled = color["transform_enable"].toBool(true);
_adjustmentEnabled = color["channelAdjustment_enable"].toBool(true);
_colorTransformV4Lonly = color["transform_v4l_only"].toBool(false);
_colorAdjustmentV4Lonly = color["channelAdjustment_v4l_only"].toBool(false);
InfoIf(!_transformEnabled , _log, "Color transformation disabled" );
InfoIf(!_adjustmentEnabled , _log, "Color adjustment disabled" );
InfoIf(_colorTransformV4Lonly , _log, "Color transformation for v4l inputs only" );
InfoIf(_colorAdjustmentV4Lonly , _log, "Color adjustment for v4l inputs only" );
// initialize the image processor factory
@@ -659,7 +526,6 @@ void Hyperion::freeObjects()
// delete components on exit of hyperion core
delete _effectEngine;
delete _device;
delete _raw2ledTransform;
delete _raw2ledAdjustment;
delete _messageForwarder;
}
@@ -805,31 +671,16 @@ void Hyperion::setImage(int priority, const Image<ColorRgb> & image, int duratio
}
}
const std::vector<std::string> & Hyperion::getTransformIds() const
{
return _raw2ledTransform->getTransformIds();
}
const std::vector<std::string> & Hyperion::getAdjustmentIds() const
{
return _raw2ledAdjustment->getAdjustmentIds();
}
ColorTransform * Hyperion::getTransform(const std::string& id)
{
return _raw2ledTransform->getTransform(id);
}
ColorAdjustment * Hyperion::getAdjustment(const std::string& id)
{
return _raw2ledAdjustment->getAdjustment(id);
}
void Hyperion::transformsUpdated()
{
update();
}
void Hyperion::adjustmentsUpdated()
{
update();
@@ -929,11 +780,7 @@ void Hyperion::update()
_ledBuffer.reserve(_hwLedCount);
_ledBuffer = priorityInfo.ledColors;
if ( _transformEnabled && (!_colorTransformV4Lonly || priorityInfo.componentId == hyperion::COMP_V4L) )
{
_raw2ledTransform->applyTransform(_ledBuffer);
}
if ( _adjustmentEnabled && (!_colorAdjustmentV4Lonly || priorityInfo.componentId == hyperion::COMP_V4L) )
if ( _adjustmentEnabled && priority < PriorityMuxer::LOWEST_PRIORITY && (!_colorAdjustmentV4Lonly || priorityInfo.componentId == hyperion::COMP_V4L) )
{
_raw2ledAdjustment->applyAdjustment(_ledBuffer);
}

View File

@@ -43,11 +43,19 @@ bool MultiColorAdjustment::verifyAdjustments() const
{
for (unsigned iLed=0; iLed<_ledAdjustments.size(); ++iLed)
{
if (_ledAdjustments[iLed] == nullptr)
ColorAdjustment * adjustment = _ledAdjustments[iLed];
if (adjustment == nullptr)
{
Warning(Logger::getInstance("ColorAdjust"), "No adjustment set for %d", iLed);
Error(Logger::getInstance("ColorAdjust"), "No adjustment set for %d", iLed);
return false;
}
if (adjustment->_rgbTransform.getBrightness() <= adjustment->_rgbTransform.getBrightnessMin() )
{
adjustment->_rgbTransform.setBrightnessMin(0);
adjustment->_rgbTransform.setBrightness(0.5);
Warning(Logger::getInstance("ColorAdjust"), "Adjustment for %d has invalid Brightness values, values set to default. (brightnessMin is bigger then brightness)", iLed);
}
}
return true;
}
@@ -84,20 +92,26 @@ void MultiColorAdjustment::applyAdjustment(std::vector<ColorRgb>& ledColors)
continue;
}
ColorRgb& color = ledColors[i];
uint8_t ored = color.red;
uint8_t ogreen = color.green;
uint8_t oblue = color.blue;
uint32_t nrng = (uint32_t) (255-color.red)*(255-color.green);
uint32_t rng = (uint32_t) (color.red) *(255-color.green);
uint32_t nrg = (uint32_t) (255-color.red)*(color.green);
uint32_t rg = (uint32_t) (color.red) *(color.green);
adjustment->_rgbTransform.transform(ored,ogreen,oblue);
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);
uint8_t black = nrng*(255-color.blue)/65025;
uint8_t red = rng *(255-color.blue)/65025;
uint8_t green = nrg *(255-color.blue)/65025;
uint8_t blue = nrng*(color.blue) /65025;
uint8_t cyan = nrg *(color.blue) /65025;
uint8_t magenta = rng *(color.blue) /65025;
uint8_t yellow = rg *(255-color.blue)/65025;
uint8_t white = rg *(color.blue) /65025;
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 OR = adjustment->_rgbBlackAdjustment.getAdjustmentR(black);
uint8_t OG = adjustment->_rgbBlackAdjustment.getAdjustmentG(black);

View File

@@ -1,94 +0,0 @@
// STL includes
#include <cassert>
// Hyperion includes
#include <utils/Logger.h>
#include "MultiColorTransform.h"
MultiColorTransform::MultiColorTransform(const unsigned ledCnt) :
_ledTransforms(ledCnt, nullptr)
{
}
MultiColorTransform::~MultiColorTransform()
{
// Clean up all the transforms
for (ColorTransform * transform : _transform)
{
delete transform;
}
}
void MultiColorTransform::addTransform(ColorTransform * transform)
{
_transformIds.push_back(transform->_id);
_transform.push_back(transform);
}
void MultiColorTransform::setTransformForLed(const std::string& id, const unsigned startLed, const unsigned endLed)
{
assert(startLed <= endLed);
assert(endLed < _ledTransforms.size());
// Get the identified transform (don't care if is nullptr)
ColorTransform * transform = getTransform(id);
for (unsigned iLed=startLed; iLed<=endLed; ++iLed)
{
_ledTransforms[iLed] = transform;
}
}
bool MultiColorTransform::verifyTransforms() const
{
for (unsigned iLed=0; iLed<_ledTransforms.size(); ++iLed)
{
if (_ledTransforms[iLed] == nullptr)
{
Warning(Logger::getInstance("ColorTransform"), "No adjustment set for %d", iLed);
return false;
}
}
return true;
}
const std::vector<std::string> & MultiColorTransform::getTransformIds()
{
return _transformIds;
}
ColorTransform* MultiColorTransform::getTransform(const std::string& id)
{
// Iterate through the unique transforms until we find the one with the given id
for (ColorTransform* transform : _transform)
{
if (transform->_id == id)
{
return transform;
}
}
// The ColorTransform was not found
return nullptr;
}
void MultiColorTransform::applyTransform(std::vector<ColorRgb>& ledColors)
{
const size_t itCnt = std::min(_ledTransforms.size(), ledColors.size());
for (size_t i=0; i<itCnt; ++i)
{
ColorTransform* transform = _ledTransforms[i];
if (transform == nullptr)
{
// No transform set for this led (do nothing)
continue;
}
ColorRgb& color = ledColors[i];
transform->_hsvTransform.transform(color.red, color.green, color.blue);
transform->_hslTransform.transform(color.red, color.green, color.blue);
color.red = transform->_rgbRedTransform.transform(color.red);
color.green = transform->_rgbGreenTransform.transform(color.green);
color.blue = transform->_rgbBlueTransform.transform(color.blue);
}
}

View File

@@ -1,64 +0,0 @@
#pragma once
// STL includes
#include <vector>
// Utils includes
#include <utils/ColorRgb.h>
// Hyperion includes
#include <hyperion/ColorTransform.h>
///
/// The LedColorTransform is responsible for performing color transformation from 'raw' colors
/// received as input to colors mapped to match the color-properties of the leds.
///
class MultiColorTransform
{
public:
MultiColorTransform(const unsigned ledCnt);
~MultiColorTransform();
/**
* Adds a new ColorTransform to this MultiColorTransform
*
* @param transform The new ColorTransform (ownership is transfered)
*/
void addTransform(ColorTransform * transform);
void setTransformForLed(const std::string& id, const unsigned startLed, const unsigned endLed);
bool verifyTransforms() const;
///
/// Returns the identifier of all the unique ColorTransform
///
/// @return The list with unique id's of the ColorTransforms
const std::vector<std::string> & getTransformIds();
///
/// Returns the pointer to the ColorTransform with the given id
///
/// @param id The identifier of the ColorTransform
///
/// @return The ColorTransform with the given id (or nullptr if it does not exist)
///
ColorTransform* getTransform(const std::string& id);
///
/// Performs the color transoformation from raw-color to led-color
///
/// @param ledColors The list with raw colors
///
void applyTransform(std::vector<ColorRgb>& ledColors);
private:
/// List with transform ids
std::vector<std::string> _transformIds;
/// List with unique ColorTransforms
std::vector<ColorTransform*> _transform;
/// List with a pointer to the ColorTransform for each individual led
std::vector<ColorTransform*> _ledTransforms;
};

View File

@@ -90,7 +90,7 @@
"append" : "edt_append_ms",
"minimum": 0,
"access" : "expert",
"propertOrder" : 4
"propertyOrder" : 4
}
},
"additionalProperties" : true
@@ -100,7 +100,7 @@
"type":"object",
"title" : "edt_conf_color_heading_title",
"required" : true,
"defaultProperties": ["imageToLedMappingType","channelAdjustment_enable","channelAdjustment","transform_enable","transform"],
"defaultProperties": ["imageToLedMappingType","channelAdjustment_enable","channelAdjustment"],
"properties":
{
"imageToLedMappingType" :
@@ -283,153 +283,61 @@
"minItems" : 3,
"maxItems" : 3,
"propertyOrder" : 10
}
},
"additionalProperties" : false
}
},
"transform_enable" :
{
"type" : "boolean",
"default" : true,
"propertyOrder" : 5
},
"transform_v4l_only" :
{
"type" : "boolean",
"default" : false,
"propertyOrder" : 6
},
"transform" :
{
"type" : "array",
"required" : true,
"propertyOrder" : 7,
"items" :
{
"type" : "object",
"required" : true,
"properties" :
{
"id" :
{
"type" : "string",
"required" : true
},
"leds" :
"brightnessMin" :
{
"type" : "string",
"required" : true
},
"hsv" : {
"type" : "object",
"properties" : {
"saturationGain" : {
"type" : "number",
"minimum" : 0.0
},
"valueGain" : {
"type" : "number",
"minimum" : 0.0
}
},
"additionalProperties" : false
},
"hsl" :
{
"type":"object",
"type" : "number",
"title" : "edt_conf_color_brightness_min",
"required" : true,
"properties":
{
"saturationGain" :
{
"type" : "number",
"required" : true,
"minimum" : 0.0
},
"luminanceGain" :
{
"type" : "number",
"required" : true,
"minimum" : 0.0
},
"luminanceMinimum" :
{
"type" : "number",
"required" : true,
"minimum" : 0.0
}
},
"additionalProperties" : false
"minimum" : 0.0,
"maximum": 1.0,
"default" : 0.0,
"step" : 0.05,
"propertyOrder" : 11
},
"red" :
"brightness" :
{
"type":"object",
"type" : "number",
"title" : "edt_conf_color_brightness",
"required" : true,
"properties":
{
"threshold" :
{
"type" : "number",
"required" : true,
"minimum" : 0.0,
"maximum": 1.0
},
"gamma" :
{
"type" : "number",
"required" : true,
"minimum" : 0.0,
"maximum": 100.0
}
},
"additionalProperties" : false
"minimum" : 0.0,
"maximum": 1.0,
"default" : 1.0,
"step" : 0.05,
"propertyOrder" : 12
},
"green" :
"gammaRed" :
{
"type":"object",
"type" : "number",
"title" : "edt_conf_color_gamma_red",
"required" : true,
"properties":
{
"threshold" :
{
"type" : "number",
"required" : true,
"minimum" : 0.0,
"maximum": 1.0
},
"gamma" :
{
"type" : "number",
"required" : true,
"minimum" : 0.0,
"maximum": 100.0
}
},
"additionalProperties" : false
"minimum" : 0.0,
"maximum": 100.0,
"default" : 1.0,
"step" : 0.1,
"propertyOrder" : 13
},
"blue" :
"gammaGreen" :
{
"type":"object",
"type" : "number",
"title" : "edt_conf_color_gamma_green",
"required" : true,
"properties":
{
"threshold" :
{
"type" : "number",
"required" : true,
"minimum" : 0.0,
"maximum": 1.0
},
"gamma" :
{
"type" : "number",
"required" : true,
"minimum" : 0.0,
"maximum": 100.0
}
},
"additionalProperties" : false
"minimum" : 0.0,
"maximum": 100.0,
"default" : 1.0,
"step" : 0.1,
"propertyOrder" : 14
},
"gammaBlue" :
{
"type" : "number",
"title" : "edt_conf_color_gamma_blue",
"required" : true,
"minimum" : 0.0,
"maximum": 100.0,
"default" : 1.0,
"step" : 0.1,
"propertyOrder" : 15
}
},
"additionalProperties" : false

View File

@@ -32,8 +32,8 @@
#include <hyperion/ImageProcessorFactory.h>
#include <hyperion/ImageProcessor.h>
#include <hyperion/MessageForwarder.h>
#include <hyperion/ColorTransform.h>
#include <hyperion/ColorAdjustment.h>
#include <utils/ColorSys.h>
#include <utils/ColorRgb.h>
#include <leddevice/LedDevice.h>
#include <HyperionConfig.h>
@@ -305,8 +305,6 @@ void JsonClientConnection::handleMessage(const QString& messageString)
handleClearCommand(message, command, tan);
else if (command == "clearall")
handleClearallCommand(message, command, tan);
else if (command == "transform")
handleTransformCommand(message, command, tan);
else if (command == "adjustment")
handleAdjustmentCommand(message, command, tan);
else if (command == "sourceselect")
@@ -627,56 +625,7 @@ void JsonClientConnection::handleServerInfoCommand(const QJsonObject&, const QSt
info["priorities"] = priorities;
info["priorities_autoselect"] = _hyperion->sourceAutoSelectEnabled();
// collect transform information
QJsonArray transformArray;
for (const std::string& transformId : _hyperion->getTransformIds())
{
const ColorTransform * colorTransform = _hyperion->getTransform(transformId);
if (colorTransform == nullptr)
{
Error(_log, "Incorrect color transform id: %s", transformId.c_str());
continue;
}
QJsonObject transform;
transform["id"] = QString::fromStdString(transformId);
transform["saturationGain"] = colorTransform->_hsvTransform.getSaturationGain();
transform["valueGain"] = colorTransform->_hsvTransform.getValueGain();
transform["saturationLGain"] = colorTransform->_hslTransform.getSaturationGain();
transform["luminanceGain"] = colorTransform->_hslTransform.getLuminanceGain();
transform["luminanceMinimum"] = colorTransform->_hslTransform.getLuminanceMinimum();
QJsonArray threshold;
threshold.append(colorTransform->_rgbRedTransform.getThreshold());
threshold.append(colorTransform->_rgbGreenTransform.getThreshold());
threshold.append(colorTransform->_rgbBlueTransform.getThreshold());
transform.insert("threshold", threshold);
QJsonArray gamma;
gamma.append(colorTransform->_rgbRedTransform.getGamma());
gamma.append(colorTransform->_rgbGreenTransform.getGamma());
gamma.append(colorTransform->_rgbBlueTransform.getGamma());
transform.insert("gamma", gamma);
QJsonArray blacklevel;
blacklevel.append(colorTransform->_rgbRedTransform.getBlacklevel());
blacklevel.append(colorTransform->_rgbGreenTransform.getBlacklevel());
blacklevel.append(colorTransform->_rgbBlueTransform.getBlacklevel());
transform.insert("blacklevel", blacklevel);
QJsonArray whitelevel;
whitelevel.append(colorTransform->_rgbRedTransform.getWhitelevel());
whitelevel.append(colorTransform->_rgbGreenTransform.getWhitelevel());
whitelevel.append(colorTransform->_rgbBlueTransform.getWhitelevel());
transform.insert("whitelevel", whitelevel);
transformArray.append(transform);
}
info["transform"] = transformArray;
// collect adjustment information
QJsonArray adjustmentArray;
for (const std::string& adjustmentId : _hyperion->getAdjustmentIds())
@@ -779,7 +728,7 @@ void JsonClientConnection::handleServerInfoCommand(const QJsonObject&, const QSt
// add HSL Value to Array
QJsonArray HSLValue;
HslTransform::rgb2hsl(priorityInfo.ledColors.begin()->red,
ColorSys::rgb2hsl(priorityInfo.ledColors.begin()->red,
priorityInfo.ledColors.begin()->green,
priorityInfo.ledColors.begin()->blue,
Hue, Saturation, Luminace);
@@ -882,83 +831,6 @@ void JsonClientConnection::handleClearallCommand(const QJsonObject& message, con
sendSuccessReply(command, tan);
}
void JsonClientConnection::handleTransformCommand(const QJsonObject& message, const QString& command, const int tan)
{
const QJsonObject & transform = message["transform"].toObject();
const QString transformId = transform["id"].toString(QString::fromStdString(_hyperion->getTransformIds().front()));
ColorTransform * colorTransform = _hyperion->getTransform(transformId.toStdString());
if (colorTransform == nullptr)
{
Warning(_log, "Incorrect transform identifier: %s", transformId.toStdString().c_str());
return;
}
if (transform.contains("saturationGain"))
{
colorTransform->_hsvTransform.setSaturationGain(transform["saturationGain"].toDouble());
}
if (transform.contains("valueGain"))
{
colorTransform->_hsvTransform.setValueGain(transform["valueGain"].toDouble());
}
if (transform.contains("saturationLGain"))
{
colorTransform->_hslTransform.setSaturationGain(transform["saturationLGain"].toDouble());
}
if (transform.contains("luminanceGain"))
{
colorTransform->_hslTransform.setLuminanceGain(transform["luminanceGain"].toDouble());
}
if (transform.contains("luminanceMinimum"))
{
colorTransform->_hslTransform.setLuminanceMinimum(transform["luminanceMinimum"].toDouble());
}
if (transform.contains("threshold"))
{
const QJsonArray & values = transform["threshold"].toArray();
colorTransform->_rgbRedTransform .setThreshold(values[0u].toDouble());
colorTransform->_rgbGreenTransform.setThreshold(values[1u].toDouble());
colorTransform->_rgbBlueTransform .setThreshold(values[2u].toDouble());
}
if (transform.contains("gamma"))
{
const QJsonArray & values = transform["gamma"].toArray();
colorTransform->_rgbRedTransform .setGamma(values[0u].toDouble());
colorTransform->_rgbGreenTransform.setGamma(values[1u].toDouble());
colorTransform->_rgbBlueTransform .setGamma(values[2u].toDouble());
}
if (transform.contains("blacklevel"))
{
const QJsonArray & values = transform["blacklevel"].toArray();
colorTransform->_rgbRedTransform .setBlacklevel(values[0u].toDouble());
colorTransform->_rgbGreenTransform.setBlacklevel(values[1u].toDouble());
colorTransform->_rgbBlueTransform .setBlacklevel(values[2u].toDouble());
}
if (transform.contains("whitelevel"))
{
const QJsonArray & values = transform["whitelevel"].toArray();
colorTransform->_rgbRedTransform .setWhitelevel(values[0u].toDouble());
colorTransform->_rgbGreenTransform.setWhitelevel(values[1u].toDouble());
colorTransform->_rgbBlueTransform .setWhitelevel(values[2u].toDouble());
}
// commit the changes
_hyperion->transformsUpdated();
sendSuccessReply(command, tan);
}
void JsonClientConnection::handleAdjustmentCommand(const QJsonObject& message, const QString& command, const int tan)
{
const QJsonObject & adjustment = message["adjustment"].toObject();
@@ -994,6 +866,64 @@ void JsonClientConnection::handleAdjustmentCommand(const QJsonObject& message, c
colorAdjustment->_rgbBlueAdjustment.setAdjustmentG(values[1u].toInt());
colorAdjustment->_rgbBlueAdjustment.setAdjustmentB(values[2u].toInt());
}
if (adjustment.contains("cyanAdjust"))
{
const QJsonArray & values = adjustment["cyanAdjust"].toArray();
colorAdjustment->_rgbCyanAdjustment.setAdjustmentR(values[0u].toInt());
colorAdjustment->_rgbCyanAdjustment.setAdjustmentG(values[1u].toInt());
colorAdjustment->_rgbCyanAdjustment.setAdjustmentB(values[2u].toInt());
}
if (adjustment.contains("magentaAdjust"))
{
const QJsonArray & values = adjustment["magentaAdjust"].toArray();
colorAdjustment->_rgbMagentaAdjustment.setAdjustmentR(values[0u].toInt());
colorAdjustment->_rgbMagentaAdjustment.setAdjustmentG(values[1u].toInt());
colorAdjustment->_rgbMagentaAdjustment.setAdjustmentB(values[2u].toInt());
}
if (adjustment.contains("yellowAdjust"))
{
const QJsonArray & values = adjustment["yellowAdjust"].toArray();
colorAdjustment->_rgbYellowAdjustment.setAdjustmentR(values[0u].toInt());
colorAdjustment->_rgbYellowAdjustment.setAdjustmentG(values[1u].toInt());
colorAdjustment->_rgbYellowAdjustment.setAdjustmentB(values[2u].toInt());
}
if (adjustment.contains("blackAdjust"))
{
const QJsonArray & values = adjustment["blackAdjust"].toArray();
colorAdjustment->_rgbBlackAdjustment.setAdjustmentR(values[0u].toInt());
colorAdjustment->_rgbBlackAdjustment.setAdjustmentG(values[1u].toInt());
colorAdjustment->_rgbBlackAdjustment.setAdjustmentB(values[2u].toInt());
}
if (adjustment.contains("whiteAdjust"))
{
const QJsonArray & values = adjustment["whiteAdjust"].toArray();
colorAdjustment->_rgbWhiteAdjustment.setAdjustmentR(values[0u].toInt());
colorAdjustment->_rgbWhiteAdjustment.setAdjustmentG(values[1u].toInt());
colorAdjustment->_rgbWhiteAdjustment.setAdjustmentB(values[2u].toInt());
}
if (adjustment.contains("gammaR"))
{
colorAdjustment->_rgbTransform.setGamma(adjustment["gammaR"].toDouble(), colorAdjustment->_rgbTransform.getGammaG(), colorAdjustment->_rgbTransform.getGammaB());
}
if (adjustment.contains("gammaG"))
{
colorAdjustment->_rgbTransform.setGamma(colorAdjustment->_rgbTransform.getGammaR(), adjustment["gammaG"].toDouble(), colorAdjustment->_rgbTransform.getGammaB());
}
if (adjustment.contains("gammaB"))
{
colorAdjustment->_rgbTransform.setGamma(colorAdjustment->_rgbTransform.getGammaR(), colorAdjustment->_rgbTransform.getGammaG(), adjustment["gammaB"].toDouble());
}
if (adjustment.contains("brightnessMin"))
{
colorAdjustment->_rgbTransform.setBrightnessMin(adjustment["brightnessMin"].toDouble());
}
if (adjustment.contains("brightness"))
{
colorAdjustment->_rgbTransform.setBrightness(adjustment["brightness"].toDouble());
}
// commit the changes
_hyperion->adjustmentsUpdated();

View File

@@ -202,13 +202,6 @@ private:
///
void handleClearallCommand(const QJsonObject & message, const QString &command, const int tan);
///
/// Handle an incoming JSON Transform message
///
/// @param message the incoming message
///
void handleTransformCommand(const QJsonObject & message, const QString &command, const int tan);
///
/// Handle an incoming JSON Adjustment message
///

View File

@@ -6,7 +6,6 @@
<file alias="schema-serverinfo">schema/schema-serverinfo.json</file>
<file alias="schema-clear">schema/schema-clear.json</file>
<file alias="schema-clearall">schema/schema-clearall.json</file>
<file alias="schema-transform">schema/schema-transform.json</file>
<file alias="schema-adjustment">schema/schema-adjustment.json</file>
<file alias="schema-effect">schema/schema-effect.json</file>
<file alias="schema-create-effect">schema/schema-create-effect.json</file>

View File

@@ -50,6 +50,91 @@
},
"minItems": 3,
"maxItems": 3
},
"yellowAdjust": {
"type": "array",
"required": false,
"items" : {
"type": "integer",
"minimum": 0,
"maximum": 255
},
"minItems": 3,
"maxItems": 3
},
"magentaAdjust": {
"type": "array",
"required": false,
"items" : {
"type": "integer",
"minimum": 0,
"maximum": 255
},
"minItems": 3,
"maxItems": 3
},
"cyanAdjust": {
"type": "array",
"required": false,
"items" : {
"type": "integer",
"minimum": 0,
"maximum": 255
},
"minItems": 3,
"maxItems": 3
},
"blackAdjust": {
"type": "array",
"required": false,
"items" : {
"type": "integer",
"minimum": 0,
"maximum": 255
},
"minItems": 3,
"maxItems": 3
},
"whiteAdjust": {
"type": "array",
"required": false,
"items" : {
"type": "integer",
"minimum": 0,
"maximum": 255
},
"minItems": 3,
"maxItems": 3
},
"gammaR": {
"type" : "number",
"required" : false,
"minimum" : 0.0,
"maximum" : 100.0
},
"gammaG": {
"type" : "number",
"required" : false,
"minimum" : 0.0,
"maximum" : 100.0
},
"gammaB": {
"type" : "number",
"required" : false,
"minimum" : 0.0,
"maximum" : 100.0
},
"brightnessMin" : {
"type" : "number",
"required" : false,
"minimum" : 0.0,
"maximum" : 1.0
},
"brightness" : {
"type" : "number",
"required" : false,
"minimum" : 0.0,
"maximum" : 1.0
}
},
"additionalProperties": false

View File

@@ -1,90 +0,0 @@
{
"type":"object",
"required":true,
"properties":{
"command": {
"type" : "string",
"required" : true,
"enum" : ["transform"]
},
"tan" : {
"type" : "integer"
},
"transform": {
"type": "object",
"required": true,
"properties": {
"id" : {
"type" : "string",
"required" : false
},
"saturationGain" : {
"type" : "number",
"required" : false,
"minimum" : 0.0
},
"valueGain" : {
"type" : "number",
"required" : false,
"minimum" : 0.0
},
"saturationLGain" : {
"type" : "number",
"required" : false,
"minimum" : 0.0
},
"luminanceGain" : {
"type" : "number",
"required" : false,
"minimum" : 0.0
},
"luminanceMinimum" : {
"type" : "number",
"required" : false,
"minimum" : 0.0
},
"threshold": {
"type": "array",
"required": false,
"items" : {
"type": "number",
"minimum": 0.0,
"maximum": 1.0
},
"minItems": 3,
"maxItems": 3
},
"gamma": {
"type": "array",
"required": false,
"items" : {
"type": "number",
"minimum": 0.0
},
"minItems": 3,
"maxItems": 3
},
"blacklevel": {
"type": "array",
"required": false,
"items" : {
"type": "number"
},
"minItems": 3,
"maxItems": 3
},
"whitelevel": {
"type": "array",
"required": false,
"items" : {
"type": "number"
},
"minItems": 3,
"maxItems": 3
}
},
"additionalProperties": false
}
},
"additionalProperties": false
}

View File

@@ -5,7 +5,7 @@
"command": {
"type" : "string",
"required" : true,
"enum" : ["color", "image", "effect", "create-effect", "delete-effect", "serverinfo", "clear", "clearall", "transform", "adjustment", "sourceselect", "config", "componentstate", "ledcolors", "logging", "processing"]
"enum" : ["color", "image", "effect", "create-effect", "delete-effect", "serverinfo", "clear", "clearall", "adjustment", "sourceselect", "config", "componentstate", "ledcolors", "logging", "processing"]
}
}
}

View File

@@ -19,9 +19,8 @@ SET(Utils_HEADERS
${CURRENT_HEADER_DIR}/PixelFormat.h
${CURRENT_HEADER_DIR}/VideoMode.h
${CURRENT_HEADER_DIR}/ImageResampler.h
${CURRENT_HEADER_DIR}/HsvTransform.h
${CURRENT_HEADER_DIR}/HslTransform.h
${CURRENT_HEADER_DIR}/RgbChannelTransform.h
${CURRENT_HEADER_DIR}/RgbTransform.h
${CURRENT_HEADER_DIR}/ColorSys.h
${CURRENT_HEADER_DIR}/RgbChannelAdjustment.h
${CURRENT_HEADER_DIR}/RgbToRgbw.h
${CURRENT_HEADER_DIR}/jsonschema/QJsonFactory.h
@@ -38,10 +37,9 @@ SET(Utils_SOURCES
${CURRENT_SOURCE_DIR}/Process.cpp
${CURRENT_SOURCE_DIR}/Logger.cpp
${CURRENT_SOURCE_DIR}/ImageResampler.cpp
${CURRENT_SOURCE_DIR}/HsvTransform.cpp
${CURRENT_SOURCE_DIR}/HslTransform.cpp
${CURRENT_SOURCE_DIR}/RgbChannelTransform.cpp
${CURRENT_SOURCE_DIR}/ColorSys.cpp
${CURRENT_SOURCE_DIR}/RgbChannelAdjustment.cpp
${CURRENT_SOURCE_DIR}/RgbTransform.cpp
${CURRENT_SOURCE_DIR}/RgbToRgbw.cpp
${CURRENT_SOURCE_DIR}/jsonschema/QJsonSchemaChecker.cpp
)
@@ -61,7 +59,7 @@ add_library(hyperion-utils
${PROFILER_SOURCE}
)
qt5_use_modules(hyperion-utils Core)
qt5_use_modules(hyperion-utils Core Gui)
target_link_libraries(hyperion-utils
${QT_LIBRARIES})

36
libsrc/utils/ColorSys.cpp Normal file
View File

@@ -0,0 +1,36 @@
#include <utils/ColorSys.h>
#include <QColor>
void ColorSys::rgb2hsl(uint8_t red, uint8_t green, uint8_t blue, uint16_t & hue, float & saturation, float & luminance)
{
QColor color(red,green,blue);
qreal h,s,l;
color.getHslF(&h,&s,&l);
hue = h;
saturation = s;
luminance = l;
}
void ColorSys::hsl2rgb(uint16_t hue, float saturation, float luminance, uint8_t & red, uint8_t & green, uint8_t & blue)
{
QColor color(QColor::fromHslF(hue,(qreal)saturation,(qreal)luminance));
red = (uint8_t)color.red();
green = (uint8_t)color.green();
blue = (uint8_t)color.blue();
}
void ColorSys::rgb2hsv(uint8_t red, uint8_t green, uint8_t blue, uint16_t & hue, uint8_t & saturation, uint8_t & value)
{
QColor color(red,green,blue);
hue = color.hsvHue();
saturation = color.hsvSaturation();
value = color.value();
}
void ColorSys::hsv2rgb(uint16_t hue, uint8_t saturation, uint8_t value, uint8_t & red, uint8_t & green, uint8_t & blue)
{
QColor color(QColor::fromHsv(hue,saturation,value));
red = (uint8_t)color.red();
green = (uint8_t)color.green();
blue = (uint8_t)color.blue();
}

View File

@@ -1,169 +0,0 @@
#include <algorithm>
#include <cmath>
#include <utils/HslTransform.h>
HslTransform::HslTransform()
: _saturationGain(1.0)
, _luminanceGain(1.0)
, _luminanceMinimum(0.0)
{
}
HslTransform::HslTransform(double saturationGain, double luminanceGain, double luminanceMinimum) :
_saturationGain(saturationGain),
_luminanceGain(luminanceGain),
_luminanceMinimum(luminanceMinimum)
{
}
HslTransform::~HslTransform()
{
}
void HslTransform::setSaturationGain(double saturationGain)
{
_saturationGain = saturationGain;
}
double HslTransform::getSaturationGain() const
{
return _saturationGain;
}
void HslTransform::setLuminanceGain(double luminanceGain)
{
_luminanceGain = luminanceGain;
}
double HslTransform::getLuminanceGain() const
{
return _luminanceGain;
}
void HslTransform::setLuminanceMinimum(double luminanceMinimum)
{
_luminanceMinimum = luminanceMinimum;
}
double HslTransform::getLuminanceMinimum() const
{
return _luminanceMinimum;
}
void HslTransform::transform(uint8_t & red, uint8_t & green, uint8_t & blue) const
{
if (_saturationGain != 1.0 || _luminanceGain != 1.0 || _luminanceMinimum != 0.0)
{
uint16_t hue;
float saturation, luminance;
rgb2hsl(red, green, blue, hue, saturation, luminance);
float s = saturation * _saturationGain;
saturation = std::min(s, 1.0f);
float l = luminance * _luminanceGain;
if (l < _luminanceMinimum)
{
saturation = 0;
l = _luminanceMinimum;
}
luminance = std::min(l, 1.0f);
hsl2rgb(hue, saturation, luminance, red, green, blue);
}
}
void HslTransform::rgb2hsl(uint8_t red, uint8_t green, uint8_t blue, uint16_t & hue, float & saturation, float & luminance)
{
float r = (float)red / 255.0f;
float g = (float)green / 255.0f;
float b = (float)blue / 255.0f;
float rgbMin = std::min(r,std::min(g,b));
float rgbMax = std::max(r,std::max(g,b));
float diff = rgbMax - rgbMin;
//luminance
luminance = (rgbMin + rgbMax) / 2.0f;
if (diff == 0.0f)
{
saturation = 0.0f;
hue = 0;
return;
}
//saturation
saturation = (luminance < 0.5f)
? (diff / (rgbMin + rgbMax))
: (diff / (2.0f - rgbMin - rgbMax));
if (rgbMax == r)
{
// start from 360 to be sure that we won't assign a negative number to the unsigned hue value
hue = 360 + 60 * (g - b) / (rgbMax - rgbMin);
if (hue > 359)
hue -= 360;
}
else if (rgbMax == g)
{
hue = 120 + 60 * (b - r) / (rgbMax - rgbMin);
}
else
{
hue = 240 + 60 * (r - g) / (rgbMax - rgbMin);
}
}
void HslTransform::hsl2rgb(uint16_t hue, float saturation, float luminance, uint8_t & red, uint8_t & green, uint8_t & blue)
{
if (saturation == 0.0f)
{
red = (uint8_t)(luminance * 255.0f);
green = (uint8_t)(luminance * 255.0f);
blue = (uint8_t)(luminance * 255.0f);
return;
}
float q = (luminance < 0.5f)
? luminance * (1.0f + saturation)
: (luminance + saturation) - (luminance * saturation);
float p = (2.0f * luminance) - q;
float h = hue / 360.0f;
float t[3];
t[0] = h + (1.0f / 3.0f);
t[1] = h;
t[2] = h - (1.0f / 3.0f);
for (int i = 0; i < 3; i++)
{
if (t[i] < 0.0f)
t[i] += 1.0f;
if (t[i] > 1.0f)
t[i] -= 1.0f;
}
float out[3];
for (int i = 0; i < 3; i++)
{
if (t[i] * 6.0f < 1.0f)
out[i] = p + (q - p) * 6.0f * t[i];
else if (t[i] * 2.0f < 1.0f)
out[i] = q;
else if (t[i] * 3.0f < 2.0f)
out[i] = p + (q - p) * ((2.0f / 3.0f) - t[i]) * 6.0f;
else out[i] = p;
}
//convert back to 0...255 range
red = (uint8_t)(out[0] * 255.0f);
green = (uint8_t)(out[1] * 255.0f);
blue = (uint8_t)(out[2] * 255.0f);
}

View File

@@ -1,144 +0,0 @@
#include <iostream>
#include <utils/HsvTransform.h>
HsvTransform::HsvTransform()
: _saturationGain(1.0)
, _valueGain(1.0)
{
}
HsvTransform::HsvTransform(double saturationGain, double valueGain) :
_saturationGain(saturationGain),
_valueGain(valueGain)
{
}
HsvTransform::~HsvTransform()
{
}
void HsvTransform::setSaturationGain(double saturationGain)
{
_saturationGain = saturationGain;
}
double HsvTransform::getSaturationGain() const
{
return _saturationGain;
}
void HsvTransform::setValueGain(double valueGain)
{
_valueGain = valueGain;
}
double HsvTransform::getValueGain() const
{
return _valueGain;
}
void HsvTransform::transform(uint8_t & red, uint8_t & green, uint8_t & blue) const
{
if (_saturationGain != 1.0 || _valueGain != 1.0)
{
uint16_t hue;
uint8_t saturation, value;
rgb2hsv(red, green, blue, hue, saturation, value);
int s = saturation * _saturationGain;
if (s > 255)
saturation = 255;
else
saturation = s;
int v = value * _valueGain;
if (v > 255)
value = 255;
else
value = v;
hsv2rgb(hue, saturation, value, red, green, blue);
}
}
void HsvTransform::rgb2hsv(uint8_t red, uint8_t green, uint8_t blue, uint16_t & hue, uint8_t & saturation, uint8_t & value)
{
uint8_t rgbMin, rgbMax;
rgbMin = red < green ? (red < blue ? red : blue) : (green < blue ? green : blue);
rgbMax = red > green ? (red > blue ? red : blue) : (green > blue ? green : blue);
value = rgbMax;
if (value == 0)
{
hue = 0;
saturation = 0;
return;
}
saturation = 255 * long(rgbMax - rgbMin) / value;
if (saturation == 0)
{
hue = 0;
return;
}
if (rgbMax == red)
{
// start from 360 to be sure that we won't assign a negative number to the unsigned hue value
hue = 360 + 60 * (green - blue) / (rgbMax - rgbMin);
if (hue > 359)
hue -= 360;
}
else if (rgbMax == green)
{
hue = 120 + 60 * (blue - red) / (rgbMax - rgbMin);
}
else
{
hue = 240 + 60 * (red - green) / (rgbMax - rgbMin);
}
}
void HsvTransform::hsv2rgb(uint16_t hue, uint8_t saturation, uint8_t value, uint8_t & red, uint8_t & green, uint8_t & blue)
{
uint8_t region, remainder, p, q, t;
if (saturation == 0)
{
red = value;
green = value;
blue = value;
return;
}
region = hue / 60;
remainder = (hue - (region * 60)) * 256 / 60;
p = (value * (255 - saturation)) >> 8;
q = (value * (255 - ((saturation * remainder) >> 8))) >> 8;
t = (value * (255 - ((saturation * (255 - remainder)) >> 8))) >> 8;
switch (region)
{
case 0:
red = value; green = t; blue = p;
break;
case 1:
red = q; green = value; blue = p;
break;
case 2:
red = p; green = value; blue = t;
break;
case 3:
red = p; green = q; blue = value;
break;
case 4:
red = t; green = p; blue = value;
break;
default:
red = value; green = p; blue = q;
break;
}
}

View File

@@ -1,101 +0,0 @@
// STL includes
#include <cmath>
// Utils includes
#include <utils/RgbChannelTransform.h>
RgbChannelTransform::RgbChannelTransform()
{
setTransform(0.0, 1.0, 0.0, 1.0);
}
RgbChannelTransform::RgbChannelTransform(double threshold, double gamma, double blacklevel, double whitelevel)
{
setTransform(threshold, gamma, blacklevel, whitelevel);
}
RgbChannelTransform::~RgbChannelTransform()
{
}
void RgbChannelTransform::setTransform(double threshold, double gamma, double blacklevel, double whitelevel)
{
_threshold = threshold;
_gamma = gamma;
_blacklevel = blacklevel;
_whitelevel = whitelevel;
initializeMapping();
}
double RgbChannelTransform::getThreshold() const
{
return _threshold;
}
void RgbChannelTransform::setThreshold(double threshold)
{
setTransform(threshold, _gamma, _blacklevel, _whitelevel);
}
double RgbChannelTransform::getGamma() const
{
return _gamma;
}
void RgbChannelTransform::setGamma(double gamma)
{
setTransform(_threshold, gamma, _blacklevel, _whitelevel);
}
double RgbChannelTransform::getBlacklevel() const
{
return _blacklevel;
}
void RgbChannelTransform::setBlacklevel(double blacklevel)
{
setTransform(_threshold, _gamma, blacklevel, _whitelevel);
}
double RgbChannelTransform::getWhitelevel() const
{
return _whitelevel;
}
void RgbChannelTransform::setWhitelevel(double whitelevel)
{
setTransform(_threshold, _gamma, _blacklevel, whitelevel);
}
void RgbChannelTransform::initializeMapping()
{
// 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;
}
}

View File

@@ -0,0 +1,114 @@
#include <iostream>
#include <cmath>
#include <utils/RgbTransform.h>
RgbTransform::RgbTransform()
{
init(1.0, 1.0, 1.0, 0.0, 1.0);
}
RgbTransform::RgbTransform(double gammaR, double gammaG, double gammaB, double brightnessLow, double brightnessHigh)
{
init(gammaR, gammaG, gammaB, brightnessLow, brightnessHigh);
}
void RgbTransform::init(double gammaR, double gammaG, double gammaB, double brightnessLow, double brightnessHigh)
{
setGamma(gammaR,gammaG,gammaB);
setBrightnessMin(brightnessLow);
setBrightness(brightnessHigh);
initializeMapping();
}
RgbTransform::~RgbTransform()
{
}
double RgbTransform::getGammaR() const
{
return _gammaR;
}
double RgbTransform::getGammaG() const
{
return _gammaG;
}
double RgbTransform::getGammaB() const
{
return _gammaB;
}
void RgbTransform::setGamma(double gammaR, double gammaG, double gammaB)
{
_gammaR = gammaR;
_gammaG = (gammaG < 0.0) ? _gammaR : gammaG;
_gammaB = (gammaB < 0.0) ? _gammaR : gammaB;
initializeMapping();
}
void RgbTransform::initializeMapping()
{
for (int i = 0; i < 256; ++i)
{
_mappingR[i] = std::min(std::max((int)(std::pow(i / 255.0, _gammaR) * 255), 0), 255);
_mappingG[i] = std::min(std::max((int)(std::pow(i / 255.0, _gammaG) * 255), 0), 255);
_mappingB[i] = std::min(std::max((int)(std::pow(i / 255.0, _gammaB) * 255), 0), 255);
}
}
double RgbTransform::getBrightnessMin() const
{
return _brightnessLow;
}
void RgbTransform::setBrightnessMin(double brightness)
{
_brightnessLow = brightness;
_sumBrightnessLow = 765.0 * ((std::pow(2.0,brightness*2)-1) / 3.0);
}
double RgbTransform::getBrightness() const
{
return _brightnessHigh;
}
void RgbTransform::setBrightness(double brightness)
{
_brightnessHigh = brightness;
_sumBrightnessHigh = 765.0 * ((std::pow(2.0,brightness*2)-1) / 3.0);
}
void RgbTransform::transform(uint8_t & red, uint8_t & green, uint8_t & blue)
{
// apply gamma
red = _mappingR[red];
green = _mappingR[green];
blue = _mappingR[blue];
//std::cout << (int)red << " " << (int)green << " " << (int)blue << " => ";
// apply brightnesss
if (red ==0) red = 1;
if (green==0) green = 1;
if (blue ==0) blue = 1;
int rgbSum = red+green+blue;
if (rgbSum > _sumBrightnessHigh)
{
double cH = _sumBrightnessHigh / rgbSum;
red *= cH;
green *= cH;
blue *= cH;
}
else if (rgbSum < _sumBrightnessLow)
{
double cL = _sumBrightnessLow / rgbSum;
red *= cL;
green *= cL;
blue *= cL;
}
//std::cout << (int)red << " " << (int)green << " " << (int)blue << std::endl;
}