Added border-ignore to the image-to-leds mapping.

Added doxygen comments.
This commit is contained in:
T. van der Zwan
2013-08-21 15:24:42 +00:00
parent b880603a30
commit 7c9ac7d151
4 changed files with 102 additions and 70 deletions

View File

@@ -7,21 +7,37 @@
using namespace hyperion;
ImageToLedsMap::ImageToLedsMap(const unsigned width, const unsigned height, const std::vector<Led>& leds) :
ImageToLedsMap::ImageToLedsMap(
const unsigned width,
const unsigned height,
const unsigned horizontalBorder,
const unsigned verticalBorder,
const std::vector<Led>& leds) :
_width(width),
_height(height),
mColorsMap()
{
// Sanity check of the size of the borders (and width and height)
assert(width > 2*verticalBorder);
assert(height > 2*horizontalBorder);
// Reserve enough space in the map for the leds
mColorsMap.reserve(leds.size());
const unsigned xOffset = verticalBorder;
const unsigned actualWidth = width - 2 * verticalBorder;
const unsigned yOffset = horizontalBorder;
const unsigned actualHeight = height - 2 * horizontalBorder;
for (const Led& led : leds)
{
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);
// Compute the index boundaries for this led
const unsigned minX_idx = xOffset + unsigned(std::round((actualWidth-1) * led.minX_frac));
const unsigned maxX_idx = xOffset + unsigned(std::round((actualWidth-1) * led.maxX_frac));
const unsigned minY_idx = yOffset + unsigned(std::round((actualHeight-1) * led.minY_frac));
const unsigned maxY_idx = yOffset + unsigned(std::round((actualHeight-1) * led.maxY_frac));
// Add all the indices in the above defined rectangle to the indices for this led
std::vector<unsigned> ledColors;
for (unsigned y = minY_idx; y<=maxY_idx && y<height; ++y)
{
@@ -30,6 +46,8 @@ ImageToLedsMap::ImageToLedsMap(const unsigned width, const unsigned height, cons
ledColors.push_back(y*width + x);
}
}
// Add the constructed vector to the map
mColorsMap.push_back(ledColors);
}
}
@@ -56,6 +74,7 @@ void ImageToLedsMap::getMeanLedColor(const RgbImage & image, std::vector<RgbColo
// Sanity check for the number of leds
assert(mColorsMap.size() == ledColors.size());
// Iterate each led and compute the mean
auto led = ledColors.begin();
for (auto ledColors = mColorsMap.begin(); ledColors != mColorsMap.end(); ++ledColors, ++led)
{
@@ -66,6 +85,7 @@ void ImageToLedsMap::getMeanLedColor(const RgbImage & image, std::vector<RgbColo
RgbColor ImageToLedsMap::calcMeanColor(const RgbImage & image, const std::vector<unsigned> & colors) const
{
// Accumulate the sum of each seperate color channel
uint_fast16_t cummRed = 0;
uint_fast16_t cummGreen = 0;
uint_fast16_t cummBlue = 0;
@@ -77,9 +97,11 @@ RgbColor ImageToLedsMap::calcMeanColor(const RgbImage & image, const std::vector
cummBlue += color.blue;
}
// Compute the average of each color channel
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 the computed color
return {avgRed, avgGreen, avgBlue};
}