2013-08-13 11:10:45 +02:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2013-08-15 21:11:02 +02:00
|
|
|
// STL includes
|
2013-11-11 10:00:37 +01:00
|
|
|
#include <cassert>
|
2013-08-15 21:11:02 +02:00
|
|
|
#include <sstream>
|
|
|
|
|
2013-08-13 11:10:45 +02:00
|
|
|
// hyperion-utils includes
|
2013-11-11 10:00:37 +01:00
|
|
|
#include <utils/Image.h>
|
2018-12-27 23:11:32 +01:00
|
|
|
#include <utils/Logger.h>
|
2013-08-13 11:10:45 +02:00
|
|
|
|
|
|
|
// hyperion includes
|
|
|
|
#include <hyperion/LedString.h>
|
|
|
|
|
|
|
|
namespace hyperion
|
|
|
|
{
|
|
|
|
|
2013-08-21 17:24:42 +02:00
|
|
|
///
|
|
|
|
/// The ImageToLedsMap holds a mapping of indices into an image to leds. It can be used to
|
|
|
|
/// calculate the average (or mean) color per led for a specific region.
|
|
|
|
///
|
|
|
|
class ImageToLedsMap
|
|
|
|
{
|
|
|
|
public:
|
2013-08-14 17:02:09 +02:00
|
|
|
|
2013-08-21 17:24:42 +02:00
|
|
|
///
|
|
|
|
/// Constructs an mapping from the absolute indices in an image to each led based on the border
|
|
|
|
/// definition given in the list of leds. The map holds absolute indices to any given image,
|
|
|
|
/// provided that it is row-oriented.
|
|
|
|
/// The mapping is created purely on size (width and height). The given borders are excluded
|
|
|
|
/// from indexing.
|
|
|
|
///
|
|
|
|
/// @param[in] width The width of the indexed image
|
|
|
|
/// @param[in] height The width of the indexed image
|
|
|
|
/// @param[in] horizontalBorder The size of the horizontal border (0=no border)
|
|
|
|
/// @param[in] verticalBorder The size of the vertical border (0=no border)
|
|
|
|
/// @param[in] leds The list with led specifications
|
|
|
|
///
|
|
|
|
ImageToLedsMap(
|
|
|
|
const unsigned width,
|
|
|
|
const unsigned height,
|
|
|
|
const unsigned horizontalBorder,
|
|
|
|
const unsigned verticalBorder,
|
|
|
|
const std::vector<Led> & leds);
|
2013-08-14 17:02:09 +02:00
|
|
|
|
2013-08-21 17:24:42 +02:00
|
|
|
///
|
|
|
|
/// Returns the width of the indexed image
|
|
|
|
///
|
|
|
|
/// @return The width of the indexed image [pixels]
|
|
|
|
///
|
|
|
|
unsigned width() const;
|
2013-08-13 11:10:45 +02:00
|
|
|
|
2013-08-21 17:24:42 +02:00
|
|
|
///
|
|
|
|
/// Returns the height of the indexed image
|
|
|
|
///
|
|
|
|
/// @return The height of the indexed image [pixels]
|
|
|
|
///
|
|
|
|
unsigned height() const;
|
2013-08-13 11:10:45 +02:00
|
|
|
|
2019-06-05 18:19:08 +02:00
|
|
|
unsigned horizontalBorder() { return _horizontalBorder; };
|
|
|
|
unsigned verticalBorder() { return _verticalBorder; };
|
2018-12-27 23:11:32 +01:00
|
|
|
|
2013-08-21 17:24:42 +02:00
|
|
|
///
|
2019-07-02 19:06:36 +02:00
|
|
|
/// Determines the mean color for each led using the mapping the image given
|
2013-08-21 17:24:42 +02:00
|
|
|
/// at construction.
|
|
|
|
///
|
2013-09-09 22:35:28 +02:00
|
|
|
/// @param[in] image The image from which to extract the led colors
|
|
|
|
///
|
2013-08-21 17:24:42 +02:00
|
|
|
/// @return ledColors The vector containing the output
|
|
|
|
///
|
2013-11-11 10:00:37 +01:00
|
|
|
template <typename Pixel_T>
|
|
|
|
std::vector<ColorRgb> getMeanLedColor(const Image<Pixel_T> & image) const
|
|
|
|
{
|
2016-12-19 23:59:50 +01:00
|
|
|
std::vector<ColorRgb> colors(_colorsMap.size(), ColorRgb{0,0,0});
|
2013-11-11 10:00:37 +01:00
|
|
|
getMeanLedColor(image, colors);
|
|
|
|
return colors;
|
|
|
|
}
|
2013-08-13 11:10:45 +02:00
|
|
|
|
2013-08-21 17:24:42 +02:00
|
|
|
///
|
|
|
|
/// Determines the mean color for each led using the mapping the image given
|
|
|
|
/// at construction.
|
|
|
|
///
|
2013-09-09 22:35:28 +02:00
|
|
|
/// @param[in] image The image from which to extract the led colors
|
2013-08-21 17:24:42 +02:00
|
|
|
/// @param[out] ledColors The vector containing the output
|
|
|
|
///
|
2013-11-11 10:00:37 +01:00
|
|
|
template <typename Pixel_T>
|
|
|
|
void getMeanLedColor(const Image<Pixel_T> & image, std::vector<ColorRgb> & ledColors) const
|
|
|
|
{
|
|
|
|
// Sanity check for the number of leds
|
2018-12-27 23:11:32 +01:00
|
|
|
//assert(_colorsMap.size() == ledColors.size());
|
|
|
|
if(_colorsMap.size() != ledColors.size())
|
|
|
|
{
|
|
|
|
Debug(Logger::getInstance("HYPERION"), "ImageToLedsMap: colorsMap.size != ledColors.size -> %d != %d", _colorsMap.size(), ledColors.size());
|
|
|
|
return;
|
|
|
|
}
|
2013-11-11 10:00:37 +01:00
|
|
|
|
|
|
|
// Iterate each led and compute the mean
|
|
|
|
auto led = ledColors.begin();
|
2019-06-05 18:19:08 +02:00
|
|
|
for (auto colors = _colorsMap.begin(); colors != _colorsMap.end(); ++colors, ++led)
|
2013-11-11 10:00:37 +01:00
|
|
|
{
|
2019-06-05 18:19:08 +02:00
|
|
|
const ColorRgb color = calcMeanColor(image, *colors);
|
2013-11-11 10:00:37 +01:00
|
|
|
*led = color;
|
|
|
|
}
|
|
|
|
}
|
2018-12-27 23:11:32 +01:00
|
|
|
|
2016-12-19 23:59:50 +01:00
|
|
|
///
|
2019-07-02 19:06:36 +02:00
|
|
|
/// Determines the uni color for each led using the mapping the image given
|
2016-12-19 23:59:50 +01:00
|
|
|
/// at construction.
|
|
|
|
///
|
|
|
|
/// @param[in] image The image from which to extract the led colors
|
|
|
|
///
|
|
|
|
/// @return ledColors The vector containing the output
|
|
|
|
///
|
|
|
|
template <typename Pixel_T>
|
|
|
|
std::vector<ColorRgb> getUniLedColor(const Image<Pixel_T> & image) const
|
|
|
|
{
|
|
|
|
std::vector<ColorRgb> colors(_colorsMap.size(), ColorRgb{0,0,0});
|
|
|
|
getUniLedColor(image, colors);
|
|
|
|
return colors;
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
2019-07-02 19:06:36 +02:00
|
|
|
/// Determines the uni color for each led using the mapping the image given
|
2016-12-19 23:59:50 +01:00
|
|
|
/// at construction.
|
|
|
|
///
|
|
|
|
/// @param[in] image The image from which to extract the led colors
|
|
|
|
/// @param[out] ledColors The vector containing the output
|
|
|
|
///
|
|
|
|
template <typename Pixel_T>
|
|
|
|
void getUniLedColor(const Image<Pixel_T> & image, std::vector<ColorRgb> & ledColors) const
|
|
|
|
{
|
|
|
|
// Sanity check for the number of leds
|
2018-12-27 23:11:32 +01:00
|
|
|
// assert(_colorsMap.size() == ledColors.size());
|
|
|
|
if(_colorsMap.size() != ledColors.size())
|
|
|
|
{
|
|
|
|
Debug(Logger::getInstance("HYPERION"), "ImageToLedsMap: colorsMap.size != ledColors.size -> %d != %d", _colorsMap.size(), ledColors.size());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-19 23:59:50 +01:00
|
|
|
|
|
|
|
// calculate uni color
|
|
|
|
const ColorRgb color = calcMeanColor(image);
|
|
|
|
std::fill(ledColors.begin(),ledColors.end(), color);
|
|
|
|
}
|
2013-08-15 21:11:02 +02:00
|
|
|
|
2013-08-21 17:24:42 +02:00
|
|
|
private:
|
|
|
|
/// The width of the indexed image
|
|
|
|
const unsigned _width;
|
|
|
|
/// The height of the indexed image
|
|
|
|
const unsigned _height;
|
2018-12-27 23:11:32 +01:00
|
|
|
|
2016-09-08 16:32:42 +02:00
|
|
|
const unsigned _horizontalBorder;
|
2018-12-27 23:11:32 +01:00
|
|
|
|
2016-09-08 16:32:42 +02:00
|
|
|
const unsigned _verticalBorder;
|
2018-12-27 23:11:32 +01:00
|
|
|
|
2013-08-21 17:24:42 +02:00
|
|
|
/// The absolute indices into the image for each led
|
2016-12-19 23:59:50 +01:00
|
|
|
std::vector<std::vector<unsigned>> _colorsMap;
|
2013-08-13 11:10:45 +02:00
|
|
|
|
2013-08-21 17:24:42 +02:00
|
|
|
///
|
|
|
|
/// Calculates the 'mean color' of the given list. This is the mean over each color-channel
|
|
|
|
/// (red, green, blue)
|
|
|
|
///
|
2013-09-09 22:35:28 +02:00
|
|
|
/// @param[in] image The image a section from which an average color must be computed
|
2013-08-21 17:24:42 +02:00
|
|
|
/// @param[in] colors The list with colors
|
|
|
|
///
|
|
|
|
/// @return The mean of the given list of colors (or black when empty)
|
|
|
|
///
|
2013-11-11 10:00:37 +01:00
|
|
|
template <typename Pixel_T>
|
|
|
|
ColorRgb calcMeanColor(const Image<Pixel_T> & image, const std::vector<unsigned> & colors) const
|
|
|
|
{
|
2019-01-06 19:49:56 +01:00
|
|
|
const auto colorVecSize = colors.size();
|
|
|
|
|
|
|
|
if (colorVecSize == 0)
|
2013-11-11 10:00:37 +01:00
|
|
|
{
|
|
|
|
return ColorRgb::BLACK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Accumulate the sum of each seperate color channel
|
|
|
|
uint_fast16_t cummRed = 0;
|
|
|
|
uint_fast16_t cummGreen = 0;
|
|
|
|
uint_fast16_t cummBlue = 0;
|
2019-01-06 19:49:56 +01:00
|
|
|
const auto& imgData = image.memptr();
|
|
|
|
|
2013-11-11 10:00:37 +01:00
|
|
|
for (const unsigned colorOffset : colors)
|
|
|
|
{
|
2019-01-06 19:49:56 +01:00
|
|
|
const auto& pixel = imgData[colorOffset];
|
2013-11-11 10:00:37 +01:00
|
|
|
cummRed += pixel.red;
|
|
|
|
cummGreen += pixel.green;
|
|
|
|
cummBlue += pixel.blue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute the average of each color channel
|
2019-01-06 19:49:56 +01:00
|
|
|
const uint8_t avgRed = uint8_t(cummRed/colorVecSize);
|
|
|
|
const uint8_t avgGreen = uint8_t(cummGreen/colorVecSize);
|
|
|
|
const uint8_t avgBlue = uint8_t(cummBlue/colorVecSize);
|
2013-11-11 10:00:37 +01:00
|
|
|
|
|
|
|
// Return the computed color
|
|
|
|
return {avgRed, avgGreen, avgBlue};
|
|
|
|
}
|
2016-12-19 23:59:50 +01:00
|
|
|
|
|
|
|
///
|
|
|
|
/// Calculates the 'mean color' over the given image. This is the mean over each color-channel
|
|
|
|
/// (red, green, blue)
|
|
|
|
///
|
|
|
|
/// @param[in] image The image a section from which an average color must be computed
|
|
|
|
///
|
|
|
|
/// @return The mean of the given list of colors (or black when empty)
|
|
|
|
///
|
|
|
|
template <typename Pixel_T>
|
|
|
|
ColorRgb calcMeanColor(const Image<Pixel_T> & image) const
|
|
|
|
{
|
|
|
|
// Accumulate the sum of each seperate color channel
|
|
|
|
uint_fast16_t cummRed = 0;
|
|
|
|
uint_fast16_t cummGreen = 0;
|
|
|
|
uint_fast16_t cummBlue = 0;
|
|
|
|
const unsigned imageSize = image.width() * image.height();
|
|
|
|
|
2019-01-06 19:49:56 +01:00
|
|
|
const auto& imgData = image.memptr();
|
|
|
|
|
2016-12-19 23:59:50 +01:00
|
|
|
for (unsigned idx=0; idx<imageSize; idx++)
|
|
|
|
{
|
2019-01-06 19:49:56 +01:00
|
|
|
const auto& pixel = imgData[idx];
|
2016-12-19 23:59:50 +01:00
|
|
|
cummRed += pixel.red;
|
|
|
|
cummGreen += pixel.green;
|
|
|
|
cummBlue += pixel.blue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute the average of each color channel
|
|
|
|
const uint8_t avgRed = uint8_t(cummRed/imageSize);
|
|
|
|
const uint8_t avgGreen = uint8_t(cummGreen/imageSize);
|
|
|
|
const uint8_t avgBlue = uint8_t(cummBlue/imageSize);
|
|
|
|
|
|
|
|
// Return the computed color
|
|
|
|
return {avgRed, avgGreen, avgBlue};
|
|
|
|
}
|
2013-08-21 17:24:42 +02:00
|
|
|
};
|
2013-08-13 11:10:45 +02:00
|
|
|
|
|
|
|
} // end namespace hyperion
|