mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2025-03-01 10:33:28 +00:00
Dominant Color support (#1569)
* Dominant Color and Mean Color Squared * Workaround - Suppress empty LED updates * Add missing text * Dominant Colors advanced * Test with fixed initial colors * Test with fixed initial colors * Support new processing values via API * ImageToLED - Add reduced pixel processing, make dominant color advanced configurable * Updates on Grabber fps setting * ImageToLedMap - Remove maptype and update test * Update dynamic cluster array allocation
This commit is contained in:
@@ -3,17 +3,26 @@
|
||||
using namespace hyperion;
|
||||
|
||||
ImageToLedsMap::ImageToLedsMap(
|
||||
unsigned width,
|
||||
unsigned height,
|
||||
unsigned horizontalBorder,
|
||||
unsigned verticalBorder,
|
||||
const std::vector<Led>& leds)
|
||||
: _width(width)
|
||||
Logger* log,
|
||||
int width,
|
||||
int height,
|
||||
int horizontalBorder,
|
||||
int verticalBorder,
|
||||
const std::vector<Led>& leds,
|
||||
int reducedPixelSetFactor,
|
||||
int accuracyLevel)
|
||||
: _log(log)
|
||||
, _width(width)
|
||||
, _height(height)
|
||||
, _horizontalBorder(horizontalBorder)
|
||||
, _verticalBorder(verticalBorder)
|
||||
, _nextPixelCount(reducedPixelSetFactor)
|
||||
, _clusterCount()
|
||||
, _colorsMap()
|
||||
{
|
||||
_nextPixelCount = reducedPixelSetFactor + 1;
|
||||
setAccuracyLevel(accuracyLevel);
|
||||
|
||||
// Sanity check of the size of the borders (and width and height)
|
||||
Q_ASSERT(_width > 2*_verticalBorder);
|
||||
Q_ASSERT(_height > 2*_horizontalBorder);
|
||||
@@ -23,10 +32,14 @@ ImageToLedsMap::ImageToLedsMap(
|
||||
// Reserve enough space in the map for the leds
|
||||
_colorsMap.reserve(leds.size());
|
||||
|
||||
const unsigned xOffset = _verticalBorder;
|
||||
const unsigned actualWidth = _width - 2 * _verticalBorder;
|
||||
const unsigned yOffset = _horizontalBorder;
|
||||
const unsigned actualHeight = _height - 2 * _horizontalBorder;
|
||||
const int xOffset = _verticalBorder;
|
||||
const int actualWidth = _width - 2 * _verticalBorder;
|
||||
const int yOffset = _horizontalBorder;
|
||||
const int actualHeight = _height - 2 * _horizontalBorder;
|
||||
|
||||
size_t totalCount = 0;
|
||||
size_t totalCapacity = 0;
|
||||
int ledCounter = 0;
|
||||
|
||||
for (const Led& led : leds)
|
||||
{
|
||||
@@ -38,10 +51,10 @@ ImageToLedsMap::ImageToLedsMap(
|
||||
}
|
||||
|
||||
// Compute the index boundaries for this led
|
||||
unsigned minX_idx = xOffset + unsigned(qRound(actualWidth * led.minX_frac));
|
||||
unsigned maxX_idx = xOffset + unsigned(qRound(actualWidth * led.maxX_frac));
|
||||
unsigned minY_idx = yOffset + unsigned(qRound(actualHeight * led.minY_frac));
|
||||
unsigned maxY_idx = yOffset + unsigned(qRound(actualHeight * led.maxY_frac));
|
||||
int minX_idx = xOffset + int32_t(qRound(actualWidth * led.minX_frac));
|
||||
int maxX_idx = xOffset + int32_t(qRound(actualWidth * led.maxX_frac));
|
||||
int minY_idx = yOffset + int32_t(qRound(actualHeight * led.minY_frac));
|
||||
int maxY_idx = yOffset + int32_t(qRound(actualHeight * led.maxY_frac));
|
||||
|
||||
// make sure that the area is at least a single led large
|
||||
minX_idx = qMin(minX_idx, xOffset + actualWidth - 1);
|
||||
@@ -56,31 +69,70 @@ ImageToLedsMap::ImageToLedsMap(
|
||||
}
|
||||
|
||||
// Add all the indices in the above defined rectangle to the indices for this led
|
||||
const auto maxYLedCount = qMin(maxY_idx, yOffset+actualHeight);
|
||||
const auto maxXLedCount = qMin(maxX_idx, xOffset+actualWidth);
|
||||
const int maxYLedCount = qMin(maxY_idx, yOffset+actualHeight);
|
||||
const int maxXLedCount = qMin(maxX_idx, xOffset+actualWidth);
|
||||
|
||||
std::vector<int32_t> ledColors;
|
||||
ledColors.reserve((size_t) maxXLedCount*maxYLedCount);
|
||||
const int realYLedCount = qAbs(maxYLedCount - minY_idx);
|
||||
const int realXLedCount = qAbs(maxXLedCount - minX_idx);
|
||||
|
||||
for (unsigned y = minY_idx; y < maxYLedCount; ++y)
|
||||
bool skipPixelProcessing {false};
|
||||
if (_nextPixelCount > 1)
|
||||
{
|
||||
for (unsigned x = minX_idx; x < maxXLedCount; ++x)
|
||||
skipPixelProcessing = true;
|
||||
}
|
||||
|
||||
size_t totalSize = static_cast<size_t>(realYLedCount * realXLedCount);
|
||||
|
||||
if (!skipPixelProcessing && totalSize > 1600)
|
||||
{
|
||||
skipPixelProcessing = true;
|
||||
_nextPixelCount = 2;
|
||||
Warning(_log, "Mapping LED/light [%d]. The current mapping area contains %d pixels which is huge. Therefore every %d pixels will be skipped. You can enable reduced processing to hide that warning.", ledCounter, totalSize, _nextPixelCount);
|
||||
}
|
||||
|
||||
std::vector<int> ledColors;
|
||||
ledColors.reserve(totalSize);
|
||||
|
||||
for (int y = minY_idx; y < maxYLedCount; y += _nextPixelCount)
|
||||
{
|
||||
for (int x = minX_idx; x < maxXLedCount; x += _nextPixelCount)
|
||||
{
|
||||
ledColors.push_back(y*width + x);
|
||||
ledColors.push_back( y * width + x);
|
||||
}
|
||||
}
|
||||
|
||||
// Add the constructed vector to the map
|
||||
_colorsMap.push_back(ledColors);
|
||||
|
||||
totalCount += ledColors.size();
|
||||
totalCapacity += ledColors.capacity();
|
||||
|
||||
ledCounter++;
|
||||
}
|
||||
Debug(_log, "Total index number is: %d (memory: %d). Reduced pixel set factor: %d, Accuracy level: %d, Image size: %d x %d, LED areas: %d",
|
||||
totalCount, totalCapacity, reducedPixelSetFactor, accuracyLevel, width, height, leds.size());
|
||||
|
||||
}
|
||||
|
||||
unsigned ImageToLedsMap::width() const
|
||||
int ImageToLedsMap::width() const
|
||||
{
|
||||
return _width;
|
||||
}
|
||||
|
||||
unsigned ImageToLedsMap::height() const
|
||||
int ImageToLedsMap::height() const
|
||||
{
|
||||
return _height;
|
||||
}
|
||||
|
||||
void ImageToLedsMap::setAccuracyLevel (int accuracyLevel)
|
||||
{
|
||||
if (accuracyLevel > 4 )
|
||||
{
|
||||
Warning(_log, "Accuracy level %d is too high, it will be set to 4", accuracyLevel);
|
||||
accuracyLevel = 4;
|
||||
}
|
||||
//Set cluster number for dominant color advanced
|
||||
_clusterCount = accuracyLevel + 1;
|
||||
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user