hyperion.ng
ImageToLedsMap.h
1 
2 #pragma once
3 
4 // STL includes
5 #include <cassert>
6 #include <sstream>
7 
8 // hyperion-utils includes
9 #include <utils/Image.h>
10 #include <utils/Logger.h>
11 
12 // hyperion includes
13 #include <hyperion/LedString.h>
14 
15 namespace hyperion
16 {
17 
23  {
24  public:
25 
40  const unsigned width,
41  const unsigned height,
42  const unsigned horizontalBorder,
43  const unsigned verticalBorder,
44  const std::vector<Led> & leds);
45 
51  unsigned width() const;
52 
58  unsigned height() const;
59 
60  unsigned horizontalBorder() { return _horizontalBorder; };
61  unsigned verticalBorder() { return _verticalBorder; };
62 
71  template <typename Pixel_T>
72  std::vector<ColorRgb> getMeanLedColor(const Image<Pixel_T> & image) const
73  {
74  std::vector<ColorRgb> colors(_colorsMap.size(), ColorRgb{0,0,0});
75  getMeanLedColor(image, colors);
76  return colors;
77  }
78 
86  template <typename Pixel_T>
87  void getMeanLedColor(const Image<Pixel_T> & image, std::vector<ColorRgb> & ledColors) const
88  {
89  // Sanity check for the number of leds
90  //assert(_colorsMap.size() == ledColors.size());
91  if(_colorsMap.size() != ledColors.size())
92  {
93  Debug(Logger::getInstance("HYPERION"), "ImageToLedsMap: colorsMap.size != ledColors.size -> %d != %d", _colorsMap.size(), ledColors.size());
94  return;
95  }
96 
97  // Iterate each led and compute the mean
98  auto led = ledColors.begin();
99  for (auto colors = _colorsMap.begin(); colors != _colorsMap.end(); ++colors, ++led)
100  {
101  const ColorRgb color = calcMeanColor(image, *colors);
102  *led = color;
103  }
104  }
105 
114  template <typename Pixel_T>
115  std::vector<ColorRgb> getUniLedColor(const Image<Pixel_T> & image) const
116  {
117  std::vector<ColorRgb> colors(_colorsMap.size(), ColorRgb{0,0,0});
118  getUniLedColor(image, colors);
119  return colors;
120  }
121 
129  template <typename Pixel_T>
130  void getUniLedColor(const Image<Pixel_T> & image, std::vector<ColorRgb> & ledColors) const
131  {
132  // Sanity check for the number of leds
133  // assert(_colorsMap.size() == ledColors.size());
134  if(_colorsMap.size() != ledColors.size())
135  {
136  Debug(Logger::getInstance("HYPERION"), "ImageToLedsMap: colorsMap.size != ledColors.size -> %d != %d", _colorsMap.size(), ledColors.size());
137  return;
138  }
139 
140 
141  // calculate uni color
142  const ColorRgb color = calcMeanColor(image);
143  std::fill(ledColors.begin(),ledColors.end(), color);
144  }
145 
146  private:
148  const unsigned _width;
150  const unsigned _height;
151 
152  const unsigned _horizontalBorder;
153 
154  const unsigned _verticalBorder;
155 
157  std::vector<std::vector<unsigned>> _colorsMap;
158 
168  template <typename Pixel_T>
169  ColorRgb calcMeanColor(const Image<Pixel_T> & image, const std::vector<unsigned> & colors) const
170  {
171  const auto colorVecSize = colors.size();
172 
173  if (colorVecSize == 0)
174  {
175  return ColorRgb::BLACK;
176  }
177 
178  // Accumulate the sum of each seperate color channel
179  uint_fast16_t cummRed = 0;
180  uint_fast16_t cummGreen = 0;
181  uint_fast16_t cummBlue = 0;
182  const auto& imgData = image.memptr();
183 
184  for (const unsigned colorOffset : colors)
185  {
186  const auto& pixel = imgData[colorOffset];
187  cummRed += pixel.red;
188  cummGreen += pixel.green;
189  cummBlue += pixel.blue;
190  }
191 
192  // Compute the average of each color channel
193  const uint8_t avgRed = uint8_t(cummRed/colorVecSize);
194  const uint8_t avgGreen = uint8_t(cummGreen/colorVecSize);
195  const uint8_t avgBlue = uint8_t(cummBlue/colorVecSize);
196 
197  // Return the computed color
198  return {avgRed, avgGreen, avgBlue};
199  }
200 
209  template <typename Pixel_T>
210  ColorRgb calcMeanColor(const Image<Pixel_T> & image) const
211  {
212  // Accumulate the sum of each seperate color channel
213  uint_fast16_t cummRed = 0;
214  uint_fast16_t cummGreen = 0;
215  uint_fast16_t cummBlue = 0;
216  const unsigned imageSize = image.width() * image.height();
217 
218  const auto& imgData = image.memptr();
219 
220  for (unsigned idx=0; idx<imageSize; idx++)
221  {
222  const auto& pixel = imgData[idx];
223  cummRed += pixel.red;
224  cummGreen += pixel.green;
225  cummBlue += pixel.blue;
226  }
227 
228  // Compute the average of each color channel
229  const uint8_t avgRed = uint8_t(cummRed/imageSize);
230  const uint8_t avgGreen = uint8_t(cummGreen/imageSize);
231  const uint8_t avgBlue = uint8_t(cummBlue/imageSize);
232 
233  // Return the computed color
234  return {avgRed, avgGreen, avgBlue};
235  }
236  };
237 
238 } // end namespace hyperion
ImageToLedsMap(const unsigned width, const unsigned height, const unsigned horizontalBorder, const unsigned verticalBorder, const std::vector< Led > &leds)
Constructs an mapping from the absolute indices in an image to each led based on the border definitio...
Definition: ImageToLedsMap.cpp:5
Provide utility methods for Hyperion class.
Definition: BlackBorderDetector.h:7
unsigned height() const
Returns the height of the indexed image.
Definition: ImageToLedsMap.cpp:83
std::vector< ColorRgb > getMeanLedColor(const Image< Pixel_T > &image) const
Determines the mean color for each led using the mapping the image given at construction.
Definition: ImageToLedsMap.h:72
unsigned height() const
Returns the height of the image.
Definition: Image.h:129
void getMeanLedColor(const Image< Pixel_T > &image, std::vector< ColorRgb > &ledColors) const
Determines the mean color for each led using the mapping the image given at construction.
Definition: ImageToLedsMap.h:87
unsigned width() const
Returns the width of the image.
Definition: Image.h:119
void getUniLedColor(const Image< Pixel_T > &image, std::vector< ColorRgb > &ledColors) const
Determines the uni color for each led using the mapping the image given at construction.
Definition: ImageToLedsMap.h:130
Pixel_T * memptr()
Returns a memory pointer to the first pixel in the image.
Definition: Image.h:208
unsigned width() const
Returns the width of the indexed image.
Definition: ImageToLedsMap.cpp:78
std::vector< ColorRgb > getUniLedColor(const Image< Pixel_T > &image) const
Determines the uni color for each led using the mapping the image given at construction.
Definition: ImageToLedsMap.h:115
static ColorRgb BLACK
&#39;Black&#39; RgbColor (0, 0, 0)
Definition: ColorRgb.h:23
Definition: Image.h:13
The ImageToLedsMap holds a mapping of indices into an image to leds.
Definition: ImageToLedsMap.h:22
Plain-Old-Data structure containing the red-green-blue color specification.
Definition: ColorRgb.h:13