Skip to content
This repository was archived by the owner on May 6, 2021. It is now read-only.

Commit 3f41d6b

Browse files
committed
Fix led mapping boundaries
1 parent 161058f commit 3f41d6b

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

libsrc/hyperion/ImageToLedsMap.cpp

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
// STL includes
32
#include <algorithm>
43
#include <cmath>
@@ -33,17 +32,35 @@ ImageToLedsMap::ImageToLedsMap(
3332

3433
for (const Led& led : leds)
3534
{
35+
// skip leds without area
36+
if ((led.maxX_frac-led.minX_frac) < 1e-6 || (led.maxY_frac-led.minY_frac) < 1e-6)
37+
{
38+
continue;
39+
}
40+
3641
// Compute the index boundaries for this led
37-
const unsigned minX_idx = xOffset + unsigned(std::round((actualWidth-1) * led.minX_frac));
38-
const unsigned maxX_idx = xOffset + unsigned(std::round((actualWidth-1) * led.maxX_frac));
39-
const unsigned minY_idx = yOffset + unsigned(std::round((actualHeight-1) * led.minY_frac));
40-
const unsigned maxY_idx = yOffset + unsigned(std::round((actualHeight-1) * led.maxY_frac));
42+
unsigned minX_idx = xOffset + unsigned(std::round(actualWidth * led.minX_frac));
43+
unsigned maxX_idx = xOffset + unsigned(std::round(actualWidth * led.maxX_frac));
44+
unsigned minY_idx = yOffset + unsigned(std::round(actualHeight * led.minY_frac));
45+
unsigned maxY_idx = yOffset + unsigned(std::round(actualHeight * led.maxY_frac));
46+
47+
// make sure that the area is at least a single led large
48+
minX_idx = std::min(minX_idx, xOffset + actualWidth - 1);
49+
if (minX_idx == maxX_idx)
50+
{
51+
maxX_idx = minX_idx + 1;
52+
}
53+
minY_idx = std::min(minY_idx, yOffset + actualHeight - 1);
54+
if (minY_idx == maxY_idx)
55+
{
56+
maxY_idx = minY_idx + 1;
57+
}
4158

4259
// Add all the indices in the above defined rectangle to the indices for this led
4360
std::vector<unsigned> ledColors;
44-
for (unsigned y = minY_idx; y<=maxY_idx && y<height; ++y)
61+
for (unsigned y = minY_idx; y<maxY_idx && y<(yOffset+actualHeight); ++y)
4562
{
46-
for (unsigned x = minX_idx; x<=maxX_idx && x<width; ++x)
63+
for (unsigned x = minX_idx; x<maxX_idx && x<(xOffset+actualWidth); ++x)
4764
{
4865
ledColors.push_back(y*width + x);
4966
}

0 commit comments

Comments
 (0)