8
8
9
9
using namespace educelab ;
10
10
11
+ namespace
12
+ {
11
13
// Conversion constants
12
- static constexpr float max_u8{std::numeric_limits<uint8_t >::max ()};
13
- static constexpr float max_u16{std::numeric_limits<uint16_t >::max ()};
14
- static constexpr float u8_to_u16{max_u16 / max_u8};
15
- static constexpr float u8_to_f32{1 .F / max_u8};
16
- static constexpr float u16_to_u8{max_u8 / max_u16};
17
- static constexpr float u16_to_f32{1 .F / max_u16};
14
+ constexpr float max_u8{std::numeric_limits<std:: uint8_t >::max ()};
15
+ constexpr float max_u16{std::numeric_limits<std:: uint16_t >::max ()};
16
+ constexpr float u8_to_u16{max_u16 / max_u8};
17
+ constexpr float u8_to_f32{1 .F / max_u8};
18
+ constexpr float u16_to_u8{max_u8 / max_u16};
19
+ constexpr float u16_to_f32{1 .F / max_u16};
18
20
19
- static inline auto size_of (const Depth d) -> std::size_t
21
+ auto size_of (const Depth d) -> std::size_t
20
22
{
21
23
switch (d) {
22
24
case Depth::None:
@@ -40,8 +42,9 @@ void depth_cast<std::uint8_t, std::uint16_t>(
40
42
{
41
43
std::uint8_t tmpI{0 };
42
44
std::memcpy (&tmpI, in, sizeof (std::uint8_t ));
43
- auto f = static_cast <float >(tmpI) * u8_to_u16;
44
- auto tmpO = static_cast <std::uint16_t >(std::max (std::min (f, max_u16), 0 .F ));
45
+ const auto f = static_cast <float >(tmpI) * u8_to_u16;
46
+ const auto tmpO =
47
+ static_cast <std::uint16_t >(std::max (std::min (f, max_u16), 0 .F ));
45
48
std::memcpy (out, &tmpO, sizeof (std::uint16_t ));
46
49
}
47
50
@@ -50,7 +53,7 @@ void depth_cast<std::uint8_t, float>(const std::byte* in, std::byte* out)
50
53
{
51
54
std::uint8_t tmpI{0 };
52
55
std::memcpy (&tmpI, in, sizeof (std::uint8_t ));
53
- auto tmpO = static_cast <float >(tmpI) * u8_to_f32;
56
+ const auto tmpO = static_cast <float >(tmpI) * u8_to_f32;
54
57
std::memcpy (out, &tmpO, sizeof (float ));
55
58
}
56
59
@@ -60,8 +63,9 @@ void depth_cast<std::uint16_t, std::uint8_t>(
60
63
{
61
64
std::uint16_t tmpI{0 };
62
65
std::memcpy (&tmpI, in, sizeof (std::uint16_t ));
63
- auto f = static_cast <float >(tmpI) * u16_to_u8;
64
- auto tmpO = static_cast <std::uint8_t >(std::max (std::min (f, max_u8), 0 .F ));
66
+ const auto f = static_cast <float >(tmpI) * u16_to_u8;
67
+ const auto tmpO =
68
+ static_cast <std::uint8_t >(std::max (std::min (f, max_u8), 0 .F ));
65
69
std::memcpy (out, &tmpO, sizeof (std::uint8_t ));
66
70
}
67
71
@@ -70,7 +74,7 @@ void depth_cast<std::uint16_t, float>(const std::byte* in, std::byte* out)
70
74
{
71
75
std::uint16_t tmpI{0 };
72
76
std::memcpy (&tmpI, in, sizeof (std::uint16_t ));
73
- auto tmpO = static_cast <float >(tmpI) * u16_to_f32;
77
+ const auto tmpO = static_cast <float >(tmpI) * u16_to_f32;
74
78
std::memcpy (out, &tmpO, sizeof (float ));
75
79
}
76
80
@@ -79,7 +83,7 @@ void depth_cast<float, std::uint8_t>(const std::byte* in, std::byte* out)
79
83
{
80
84
float tmpI{0 };
81
85
std::memcpy (&tmpI, in, sizeof (float ));
82
- auto tmpO = static_cast <std::uint8_t >(
86
+ const auto tmpO = static_cast <std::uint8_t >(
83
87
std::max (std::min (tmpI * max_u8, max_u8), 0 .F ));
84
88
std::memcpy (out, &tmpO, sizeof (std::uint8_t ));
85
89
}
@@ -89,7 +93,7 @@ void depth_cast<float, std::uint16_t>(const std::byte* in, std::byte* out)
89
93
{
90
94
float tmpI{0 };
91
95
std::memcpy (&tmpI, in, sizeof (float ));
92
- auto tmpO = static_cast <std::uint16_t >(
96
+ const auto tmpO = static_cast <std::uint16_t >(
93
97
std::max (std::min (tmpI * max_u16, max_u16), 0 .F ));
94
98
std::memcpy (out, &tmpO, sizeof (std::uint16_t ));
95
99
}
@@ -98,13 +102,13 @@ template <typename TIn, typename TOut>
98
102
void pixel_cast (const std::byte* in, std::byte* out, const std::size_t cns)
99
103
{
100
104
for (std::size_t idx{0 }; idx < cns; idx++) {
101
- auto i_idx = idx * sizeof (TIn);
102
- auto o_idx = idx * sizeof (TOut);
105
+ const auto i_idx = idx * sizeof (TIn);
106
+ const auto o_idx = idx * sizeof (TOut);
103
107
depth_cast<TIn, TOut>(&in[i_idx], &out[o_idx]);
104
108
}
105
109
}
106
110
107
- static void convert_pixel (
111
+ void convert_pixel (
108
112
const std::byte* in,
109
113
const Depth inType,
110
114
std::byte* out,
@@ -147,6 +151,7 @@ static void convert_pixel(
147
151
throw std::runtime_error (" Conversion not supported." );
148
152
}
149
153
}
154
+ } // namespace
150
155
151
156
Image::Image (
152
157
const std::size_t height,
@@ -205,8 +210,8 @@ auto Image::Convert(const Image& i, const Depth type) -> Image
205
210
// Iterate over original image data
206
211
for (std::size_t y{0 }; y < i.h_ ; y++) {
207
212
for (std::size_t x{0 }; x < i.w_ ; x++) {
208
- auto i_idx = i.unravel_ (y, x);
209
- auto r_idx = result.unravel_ (y, x);
213
+ const auto i_idx = i.unravel_ (y, x);
214
+ const auto r_idx = result.unravel_ (y, x);
210
215
convert_pixel (
211
216
&i.data_ [i_idx], i.type (), &result.data_ [r_idx], type, i.cns_ );
212
217
}
@@ -220,15 +225,16 @@ auto Image::Gamma(const Image& i, const float gamma) -> Image
220
225
auto result = Convert (i, Depth::F32);
221
226
for (std::size_t y{0 }; y < result.h_ ; y++) {
222
227
for (std::size_t x{0 }; x < result.w_ ; x++) {
223
- auto idx = result.unravel_ (y, x);
228
+ const auto idx = result.unravel_ (y, x);
224
229
for (std::size_t c{0 }; c < result.cns_ ; c++) {
225
- auto o = c * sizeof (float );
230
+ const auto o = c * sizeof (float );
226
231
auto * v = reinterpret_cast <float *>(&result.data_ [idx + o]);
227
232
*v = std::pow (*v, 1 .F / gamma);
228
233
}
229
234
}
230
235
}
231
- return Convert (result, i.type ());
236
+ result = Convert (result, i.type ());
237
+ return result;
232
238
}
233
239
234
240
auto Image::convert (const Depth type) const -> Image
0 commit comments