ImageToLedsMap.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include <hyperion/ImageToLedsMap.h>
  2. using namespace hyperion;
  3. ImageToLedsMap::ImageToLedsMap(
  4. Logger* log,
  5. int width,
  6. int height,
  7. int horizontalBorder,
  8. int verticalBorder,
  9. const std::vector<Led>& leds,
  10. int reducedPixelSetFactor,
  11. int accuracyLevel)
  12. : _log(log)
  13. , _width(width)
  14. , _height(height)
  15. , _horizontalBorder(horizontalBorder)
  16. , _verticalBorder(verticalBorder)
  17. , _nextPixelCount(reducedPixelSetFactor)
  18. , _clusterCount()
  19. , _colorsMap()
  20. {
  21. _nextPixelCount = reducedPixelSetFactor + 1;
  22. setAccuracyLevel(accuracyLevel);
  23. // Sanity check of the size of the borders (and width and height)
  24. Q_ASSERT(_width > 2*_verticalBorder);
  25. Q_ASSERT(_height > 2*_horizontalBorder);
  26. Q_ASSERT(_width < 10000);
  27. Q_ASSERT(_height < 10000);
  28. // Reserve enough space in the map for the leds
  29. _colorsMap.reserve(leds.size());
  30. const int xOffset = _verticalBorder;
  31. const int actualWidth = _width - 2 * _verticalBorder;
  32. const int yOffset = _horizontalBorder;
  33. const int actualHeight = _height - 2 * _horizontalBorder;
  34. size_t totalCount = 0;
  35. size_t totalCapacity = 0;
  36. int ledCounter = 0;
  37. for (const Led& led : leds)
  38. {
  39. // skip leds without area
  40. if ((led.maxX_frac-led.minX_frac) < 1e-6 || (led.maxY_frac-led.minY_frac) < 1e-6)
  41. {
  42. _colorsMap.emplace_back();
  43. continue;
  44. }
  45. // Compute the index boundaries for this led
  46. int minX_idx = xOffset + int32_t(qRound(actualWidth * led.minX_frac));
  47. int maxX_idx = xOffset + int32_t(qRound(actualWidth * led.maxX_frac));
  48. int minY_idx = yOffset + int32_t(qRound(actualHeight * led.minY_frac));
  49. int maxY_idx = yOffset + int32_t(qRound(actualHeight * led.maxY_frac));
  50. // make sure that the area is at least a single led large
  51. minX_idx = qMin(minX_idx, xOffset + actualWidth - 1);
  52. if (minX_idx == maxX_idx)
  53. {
  54. maxX_idx++;
  55. }
  56. minY_idx = qMin(minY_idx, yOffset + actualHeight - 1);
  57. if (minY_idx == maxY_idx)
  58. {
  59. maxY_idx++;
  60. }
  61. // Add all the indices in the above defined rectangle to the indices for this led
  62. const int maxYLedCount = qMin(maxY_idx, yOffset+actualHeight);
  63. const int maxXLedCount = qMin(maxX_idx, xOffset+actualWidth);
  64. const int realYLedCount = qAbs(maxYLedCount - minY_idx);
  65. const int realXLedCount = qAbs(maxXLedCount - minX_idx);
  66. bool skipPixelProcessing {false};
  67. if (_nextPixelCount > 1)
  68. {
  69. skipPixelProcessing = true;
  70. }
  71. size_t totalSize = static_cast<size_t>(realYLedCount * realXLedCount);
  72. if (!skipPixelProcessing && totalSize > 1600)
  73. {
  74. skipPixelProcessing = true;
  75. _nextPixelCount = 2;
  76. 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);
  77. }
  78. std::vector<int> ledColors;
  79. ledColors.reserve(totalSize);
  80. for (int y = minY_idx; y < maxYLedCount; y += _nextPixelCount)
  81. {
  82. for (int x = minX_idx; x < maxXLedCount; x += _nextPixelCount)
  83. {
  84. ledColors.push_back( y * width + x);
  85. }
  86. }
  87. // Add the constructed vector to the map
  88. _colorsMap.push_back(ledColors);
  89. totalCount += ledColors.size();
  90. totalCapacity += ledColors.capacity();
  91. ledCounter++;
  92. }
  93. 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",
  94. totalCount, totalCapacity, reducedPixelSetFactor, accuracyLevel, width, height, leds.size());
  95. }
  96. int ImageToLedsMap::width() const
  97. {
  98. return _width;
  99. }
  100. int ImageToLedsMap::height() const
  101. {
  102. return _height;
  103. }
  104. void ImageToLedsMap::setAccuracyLevel (int accuracyLevel)
  105. {
  106. if (accuracyLevel > 4 )
  107. {
  108. Warning(_log, "Accuracy level %d is too high, it will be set to 4", accuracyLevel);
  109. accuracyLevel = 4;
  110. }
  111. //Set cluster number for dominant color advanced
  112. _clusterCount = accuracyLevel + 1;
  113. }