2013-07-26 22:38:34 +02:00
|
|
|
|
|
|
|
// STL includes
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
// hyperion includes
|
2013-08-13 11:10:45 +02:00
|
|
|
#include "ImageToLedsMap.h"
|
2013-07-26 22:38:34 +02:00
|
|
|
|
2013-08-13 11:10:45 +02:00
|
|
|
using namespace hyperion;
|
2013-07-26 22:38:34 +02:00
|
|
|
|
2013-08-14 17:02:09 +02:00
|
|
|
ImageToLedsMap::ImageToLedsMap(const unsigned width, const unsigned height, const std::vector<Led>& leds) :
|
|
|
|
_width(width),
|
|
|
|
_height(height),
|
|
|
|
mColorsMap()
|
2013-07-26 22:38:34 +02:00
|
|
|
{
|
2013-08-14 17:02:09 +02:00
|
|
|
// Reserve enough space in the map for the leds
|
|
|
|
mColorsMap.reserve(leds.size());
|
2013-07-26 22:38:34 +02:00
|
|
|
|
2013-08-14 17:02:09 +02:00
|
|
|
for (const Led& led : leds)
|
2013-07-26 22:38:34 +02:00
|
|
|
{
|
2013-08-14 17:02:09 +02:00
|
|
|
const unsigned minX_idx = unsigned(width * led.minX_frac);
|
|
|
|
const unsigned maxX_idx = unsigned(width * led.maxX_frac);
|
|
|
|
const unsigned minY_idx = unsigned(height * led.minY_frac);
|
|
|
|
const unsigned maxY_idx = unsigned(height * led.maxY_frac);
|
2013-07-26 22:38:34 +02:00
|
|
|
|
2013-08-14 17:02:09 +02:00
|
|
|
std::vector<unsigned> ledColors;
|
|
|
|
for (unsigned y = minY_idx; y<=maxY_idx && y<height; ++y)
|
2013-07-26 22:38:34 +02:00
|
|
|
{
|
2013-08-14 17:02:09 +02:00
|
|
|
for (unsigned x = minX_idx; x<=maxX_idx && x<width; ++x)
|
2013-07-26 22:38:34 +02:00
|
|
|
{
|
2013-08-14 17:02:09 +02:00
|
|
|
ledColors.push_back(y*width + x);
|
2013-07-26 22:38:34 +02:00
|
|
|
}
|
|
|
|
}
|
2013-08-14 17:02:09 +02:00
|
|
|
mColorsMap.push_back(ledColors);
|
2013-07-26 22:38:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-14 17:02:09 +02:00
|
|
|
unsigned ImageToLedsMap::width() const
|
2013-07-26 22:38:34 +02:00
|
|
|
{
|
2013-08-14 17:02:09 +02:00
|
|
|
return _width;
|
|
|
|
}
|
2013-07-26 22:38:34 +02:00
|
|
|
|
2013-08-14 17:02:09 +02:00
|
|
|
unsigned ImageToLedsMap::height() const
|
|
|
|
{
|
|
|
|
return _height;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<RgbColor> ImageToLedsMap::getMeanLedColor(const RgbImage & image) const
|
|
|
|
{
|
|
|
|
std::vector<RgbColor> colors(mColorsMap.size(), RgbColor::BLACK);
|
|
|
|
getMeanLedColor(image, colors);
|
2013-07-26 22:38:34 +02:00
|
|
|
return colors;
|
|
|
|
}
|
|
|
|
|
2013-08-14 17:02:09 +02:00
|
|
|
void ImageToLedsMap::getMeanLedColor(const RgbImage & image, std::vector<RgbColor> & ledColors) const
|
2013-08-13 11:10:45 +02:00
|
|
|
{
|
|
|
|
// Sanity check for the number of leds
|
|
|
|
assert(mColorsMap.size() == ledColors.size());
|
|
|
|
|
|
|
|
auto led = ledColors.begin();
|
|
|
|
for (auto ledColors = mColorsMap.begin(); ledColors != mColorsMap.end(); ++ledColors, ++led)
|
|
|
|
{
|
2013-08-14 17:02:09 +02:00
|
|
|
const RgbColor color = calcMeanColor(image, *ledColors);
|
2013-08-13 11:10:45 +02:00
|
|
|
*led = color;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-14 17:02:09 +02:00
|
|
|
RgbColor ImageToLedsMap::calcMeanColor(const RgbImage & image, const std::vector<unsigned> & colors) const
|
2013-07-26 22:38:34 +02:00
|
|
|
{
|
|
|
|
uint_fast16_t cummRed = 0;
|
|
|
|
uint_fast16_t cummGreen = 0;
|
|
|
|
uint_fast16_t cummBlue = 0;
|
2013-08-14 17:02:09 +02:00
|
|
|
for (const unsigned colorOffset : colors)
|
2013-07-26 22:38:34 +02:00
|
|
|
{
|
2013-08-14 17:02:09 +02:00
|
|
|
const RgbColor& color = image.memptr()[colorOffset];
|
|
|
|
cummRed += color.red;
|
|
|
|
cummGreen += color.green;
|
|
|
|
cummBlue += color.blue;
|
2013-07-26 22:38:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const uint8_t avgRed = uint8_t(cummRed/colors.size());
|
|
|
|
const uint8_t avgGreen = uint8_t(cummGreen/colors.size());
|
|
|
|
const uint8_t avgBlue = uint8_t(cummBlue/colors.size());
|
|
|
|
|
|
|
|
return {avgRed, avgGreen, avgBlue};
|
|
|
|
}
|