Skip to content

Commit 9f475b6

Browse files
Change from int16_t back to int as it works better
1 parent a21c638 commit 9f475b6

File tree

1 file changed

+34
-31
lines changed

1 file changed

+34
-31
lines changed

cpp/visualmesh/engine/cpu/bayer.hpp

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -42,43 +42,46 @@ namespace engine {
4242
// These coefficent values are scaled by 64 (divide by 64 after multiplying out)
4343
// clang-format off
4444
// G at red locations
45-
constexpr std::array<int16_t, 25> G_R = {{ 0, 0, -8, 0, 0,
46-
0, 0, 16, 0, 0,
47-
-8, 16, 32, 16, -8,
48-
0, 0, 16, 0, 0,
49-
0, 0, -8, 0, 0 }};
45+
constexpr std::array<int, 25> G_R = {{ 0, 0, -8, 0, 0,
46+
0, 0, 16, 0, 0,
47+
-8, 16, 32, 16, -8,
48+
0, 0, 16, 0, 0,
49+
0, 0, -8, 0, 0 }};
5050
// G at blue locations
51-
constexpr std::array<int16_t, 25> G_B = G_R;
51+
constexpr std::array<int, 25> G_B = G_R;
5252

5353

5454
// R at blue locations
55-
constexpr std::array<int16_t, 25> R_B = {{ 0, 0, -12, 0, 0,
56-
0, 16, 0, 16, 0,
57-
-12, 0, 48, 0, -12,
58-
0, 16, 0, 16, 0,
59-
0, 0, -12, 0, 0 }};
55+
constexpr std::array<int, 25> R_B = {{ 0, 0, -12, 0, 0,
56+
0, 16, 0, 16, 0,
57+
-12, 0, 48, 0, -12,
58+
0, 16, 0, 16, 0,
59+
0, 0, -12, 0, 0 }};
6060
// B at red locations
61-
constexpr std::array<int16_t, 25> B_R = R_B;
61+
constexpr std::array<int, 25> B_R = R_B;
6262

6363
// R at green locations on red rows
64-
constexpr std::array<int16_t, 25> R_GR = {{ 0, 0, 4, 0, 0,
65-
0, -8, 0, -8, 0,
66-
-8, 32, 40, 32, -8,
67-
0, -8, 0, -8, 0,
68-
0, 0, 4, 0, 0 }};
64+
constexpr std::array<int, 25> R_GR = {{ 0, 0, 4, 0, 0,
65+
0, -8, 0, -8, 0,
66+
-8, 32, 40, 32, -8,
67+
0, -8, 0, -8, 0,
68+
0, 0, 4, 0, 0 }};
6969
// Blue at green locations on red rows
70-
constexpr std::array<int16_t, 25> B_GB = R_GR;
70+
constexpr std::array<int, 25> B_GB = R_GR;
7171

7272
// Red at green locations on blue rows
73-
constexpr std::array<int16_t, 25> R_GB = {{ 0, 0, -8, 0, 0,
74-
0, -8, 32, -8, 0,
75-
4, 0, 40, 0, 4,
76-
0, -8, 32, -8, 0,
77-
0, 0, -8, 0, 0 }};
73+
constexpr std::array<int, 25> R_GB = {{ 0, 0, -8, 0, 0,
74+
0, -8, 32, -8, 0,
75+
4, 0, 40, 0, 4,
76+
0, -8, 32, -8, 0,
77+
0, 0, -8, 0, 0 }};
7878
// Blue at green locations on red rows
79-
constexpr std::array<int16_t, 25> B_GR = R_GB;
79+
constexpr std::array<int, 25> B_GR = R_GB;
8080
// clang-format on
8181

82+
// Scale factor
83+
constexpr int scale = 64;
84+
8285
/**
8386
* @brief Demosaics the patch into an rgb pixel value
8487
*
@@ -90,13 +93,13 @@ namespace engine {
9093
* @return an rgb value for the image patch
9194
*/
9295
template <typename Scalar>
93-
inline vec4<Scalar> demosaic(const std::array<int16_t, 5 * 5>& p, const BayerPixelType type) {
94-
vec4<int16_t> output;
96+
inline vec4<Scalar> demosaic(const std::array<int, 5 * 5>& p, const BayerPixelType type) {
97+
vec4<int> output;
9598
switch (type) {
96-
case R: output = vec4<int16_t>{{p[12], dot(p, G_R) / 64, dot(p, B_R) / 64, 255}}; break;
97-
case GR: output = vec4<int16_t>{{dot(p, R_GR) / 64, p[12], dot(p, B_GR) / 64, 255}}; break;
98-
case GB: output = vec4<int16_t>{{dot(p, R_GB) / 64, p[12], dot(p, B_GB) / 64, 255}}; break;
99-
case B: output = vec4<int16_t>{{dot(p, R_B) / 64, dot(p, G_B) / 64, p[12], 255}}; break;
99+
case R: output = vec4<int>{{p[12], dot(p, G_R) / scale, dot(p, B_R) / scale, 255}}; break;
100+
case GR: output = vec4<int>{{dot(p, R_GR) / scale, p[12], dot(p, B_GR) / scale, 255}}; break;
101+
case GB: output = vec4<int>{{dot(p, R_GB) / scale, p[12], dot(p, B_GB) / scale, 255}}; break;
102+
case B: output = vec4<int>{{dot(p, R_B) / scale, dot(p, G_B) / scale, p[12], 255}}; break;
100103
default: throw std::runtime_error("Unknown bayer pixel type"); break;
101104
}
102105

@@ -127,7 +130,7 @@ namespace engine {
127130
int y_s = px[1] - 2;
128131

129132
// Read the image patch into a flat array
130-
std::array<int16_t, 5 * 5> patch{};
133+
std::array<int, 5 * 5> patch{};
131134
for (int y = 0; y < 5; ++y) {
132135
int y_c = std::min(std::max(y_s + y, 0), dimensions[1] - 1);
133136
for (int x = 0; x < 5; ++x) {

0 commit comments

Comments
 (0)