47 #ifndef _MIRA_ENDIAN_H_ 48 #define _MIRA_ENDIAN_H_ 50 #include <type_traits> 53 #include <boost/version.hpp> 54 #if (BOOST_VERSION >= 107100) 56 #include <boost/predef/other/endian.h> 57 #if BOOST_ENDIAN_LITTLE_BYTE 58 #define MIRA_LITTLE_ENDIAN 59 #undef MIRA_BIG_ENDIAN 60 #elif BOOST_ENDIAN_BIG_BYTE 61 #define MIRA_BIG_ENDIAN 62 #undef MIRA_LITTLE_ENDIAN 64 #error "Impossible to determine the endianness of the system" 67 #include <boost/detail/endian.hpp> 68 #if defined(BOOST_LITTLE_ENDIAN) 69 #define MIRA_LITTLE_ENDIAN 70 #undef MIRA_BIG_ENDIAN 71 #elif defined(BOOST_BIG_ENDIAN) 72 #define MIRA_BIG_ENDIAN 73 #undef MIRA_LITTLE_ENDIAN 75 #error "Impossible to determine the endianness of the system" 94 #if defined(MIRA_LITTLE_ENDIAN) 96 #elif defined(MIRA_BIG_ENDIAN) 99 # error "Impossible to determine the endianness of the system" 114 inline void swapBytes(
const char* iSrc,
char* oDest)
116 static_assert(size!=size,
"swapBytes is not specialized for this size");
125 inline void swapBytes<1>(
const char* iSrc,
char* oDest)
134 inline void swapBytes<2>(
const char* iSrc,
char* oDest)
138 uint16* dest =
reinterpret_cast<uint16*
>(oDest);
139 const uint16* src =
reinterpret_cast<const uint16*
>(iSrc);
140 *dest = (*src >> 8) | (*src << 8);
147 inline void swapBytes<4>(
const char* iSrc,
char* oDest)
151 uint32* dest =
reinterpret_cast<uint32*
>(oDest);
152 const uint32* src =
reinterpret_cast<const uint32*
>(iSrc);
153 *dest = (*src >> 24) |
154 ((*src & 0x00ff0000) >> 8) |
155 ((*src & 0x0000ff00) << 8) |
163 inline void swapBytes<8>(
const char* iSrc,
char* oDest)
167 uint64* dest =
reinterpret_cast<uint64*
>(oDest);
168 const uint64* src =
reinterpret_cast<const uint64*
>(iSrc);
169 *dest = (((*src & 0xff00000000000000ull) >> 56) |
170 ((*src & 0x00ff000000000000ull) >> 40) |
171 ((*src & 0x0000ff0000000000ull) >> 24) |
172 ((*src & 0x000000ff00000000ull) >> 8) |
173 ((*src & 0x00000000ff000000ull) << 8) |
174 ((*src & 0x0000000000ff0000ull) << 24) |
175 ((*src & 0x000000000000ff00ull) << 40) |
176 ((*src & 0x00000000000000ffull) << 56));
188 #if defined(MIRA_BIG_ENDIAN) 192 #elif defined(MIRA_LITTLE_ENDIAN) 209 #if defined(MIRA_BIG_ENDIAN) 214 #elif defined(MIRA_LITTLE_ENDIAN) 215 uint32 mantissa1 :32;
216 uint32 mantissa0 :20;
233 template <
typename T>
266 static_assert(std::is_arithmetic<T>::value,
267 "hostToNetwork accepts arithmetic types only");
269 #if defined(MIRA_BIG_ENDIAN) 273 Private::swapBytes<sizeof(T)>(
reinterpret_cast<const char*
>(&value),
274 reinterpret_cast<char*>(&r));
285 #if defined(MIRA_BIG_ENDIAN) 289 uint8* ptr =
reinterpret_cast<uint8*
>(&r);
291 union Private::IEEE754Float f;
294 ptr[0] = (f.ieee.negative << 7) | ((f.ieee.exponent >> 1) & 0x7F);
295 ptr[1] = ((f.ieee.exponent & 0x01) << 7) | ((f.ieee.mantissa >> 16) & 0x7F);
296 ptr[2] = (f.ieee.mantissa >> 8) & 0xFF;
297 ptr[3] = (f.ieee.mantissa) & 0xFF;
309 #if defined(MIRA_BIG_ENDIAN) 313 uint8* ptr =
reinterpret_cast<uint8*
>(&r);
315 union Private::IEEE754Double d;
318 ptr[0] = (d.ieee.negative << 7) + ((d.ieee.exponent >> 4) & 0x7F);
319 ptr[1] = (((d.ieee.exponent) << 4) & 0xF0) + ((d.ieee.mantissa0 >> 16) & 0x0F);
320 ptr[2] = (d.ieee.mantissa0 >> 8) & 0xFF;
321 ptr[3] = (d.ieee.mantissa0) & 0xFF;
322 ptr[4] = (d.ieee.mantissa1 >> 24) & 0xFF;
323 ptr[5] = (d.ieee.mantissa1 >> 16) & 0xFF;
324 ptr[6] = (d.ieee.mantissa1 >> 8) & 0xFF;
325 ptr[7] = (d.ieee.mantissa1) & 0xFF;
339 static_assert(std::is_arithmetic<T>::value,
340 "networkToHost accepts arithmetic types only");
342 #if defined(MIRA_BIG_ENDIAN) 346 Private::swapBytes<sizeof(T)>(
reinterpret_cast<const char*
>(&value),
347 reinterpret_cast<char*>(&r));
358 #if defined(MIRA_BIG_ENDIAN) 359 return *
reinterpret_cast<const float*
>(&value);
361 const uint8* ptr =
reinterpret_cast<const uint8*
>(&value);
363 union Private::IEEE754Float f;
364 f.ieee.negative = ptr[0] >> 7;
365 f.ieee.exponent = ((ptr[0] << 1) & 0xFE) + ((ptr[1] >> 7) & 0x01);
366 f.ieee.mantissa = (ptr[3] & 0xFF) + ((ptr[2] << 8) & 0xFF00) +
367 (((ptr[1] & 0x7F) << 16) & 0xFF0000);
379 #if defined(MIRA_BIG_ENDIAN) 380 return *
reinterpret_cast<const double*
>(&value);
382 const uint8* ptr =
reinterpret_cast<const uint8*
>(&value);
384 union Private::IEEE754Double d;
385 d.ieee.negative = ptr[0] >> 7;
386 d.ieee.exponent = ((ptr[0] << 4) & 0x7F0) + ((ptr[1] >> 4) & 0x0F);
387 d.ieee.mantissa0 = (ptr[3] & 0xFF) + ((ptr[2] << 8) & 0xFF00) +
388 (((ptr[1] & 0x0F) << 16) & 0xFF0000);
389 d.ieee.mantissa1 = (ptr[7] & 0xFF) + ((ptr[6] << 8) & 0xFF00) +
390 ((ptr[5] << 16) & 0xFF0000) + ((ptr[4] << 24) & 0xFF000000);
Typedefs for OS independent basic data types.
T Type
Definition: Endian.h:236
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
Endian
Enum that specifies the endianness of the host system.
Definition: Endian.h:89
uint32 Type
Definition: Endian.h:245
uint64 Type
Definition: Endian.h:254
T networkToHost(const typename NetworkTypeTrait< T >::Type &value)
Converts a value from network byte order to host byte order.
Definition: Endian.h:337
NetworkTypeTrait< double >::Type hostToNetwork< double >(const double &value)
Specialization for double.
Definition: Endian.h:307
NetworkTypeTrait< float >::Type hostToNetwork< float >(const float &value)
Specialization for float.
Definition: Endian.h:283
NetworkTypeTrait< T >::Type hostToNetwork(const T &value)
Converts a value from host byte order to network byte order.
Definition: Endian.h:264
Trait that specifies the conversion type of a given data type.
Definition: Endian.h:234