ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
endian.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <algorithm>
11 
12 namespace colmap {
13 
14 // Reverse the order of each byte.
15 template <typename T>
16 T ReverseBytes(const T& data);
17 
18 // Check the order in which bytes are stored in computer memory.
19 bool IsLittleEndian();
20 bool IsBigEndian();
21 
22 // Convert data between endianness and the native format. Note that, for float
23 // and double types, these functions are only valid if the format is IEEE-754.
24 // This is the case for pretty much most processors.
25 template <typename T>
26 T LittleEndianToNative(const T x);
27 template <typename T>
28 T BigEndianToNative(const T x);
29 template <typename T>
30 T NativeToLittleEndian(const T x);
31 template <typename T>
32 T NativeToBigEndian(const T x);
33 
34 // Read data in little endian format for cross-platform support.
35 template <typename T>
36 T ReadBinaryLittleEndian(std::istream* stream);
37 template <typename T>
38 void ReadBinaryLittleEndian(std::istream* stream, std::vector<T>* data);
39 
40 // Write data in little endian format for cross-platform support.
41 template <typename T>
42 void WriteBinaryLittleEndian(std::ostream* stream, const T& data);
43 template <typename T>
44 void WriteBinaryLittleEndian(std::ostream* stream, const std::vector<T>& data);
45 
47 // Implementation
49 
50 template <typename T>
51 T ReverseBytes(const T& data) {
52  T data_reversed = data;
53  std::reverse(reinterpret_cast<char*>(&data_reversed),
54  reinterpret_cast<char*>(&data_reversed) + sizeof(T));
55  return data_reversed;
56 }
57 
58 inline bool IsLittleEndian() {
59 #ifdef BOOST_BIG_ENDIAN
60  return false;
61 #else
62  return true;
63 #endif
64 }
65 
66 inline bool IsBigEndian() {
67 #ifdef BOOST_BIG_ENDIAN
68  return true;
69 #else
70  return false;
71 #endif
72 }
73 
74 template <typename T>
75 T LittleEndianToNative(const T x) {
76  if (IsLittleEndian()) {
77  return x;
78  } else {
79  return ReverseBytes(x);
80  }
81 }
82 
83 template <typename T>
84 T BigEndianToNative(const T x) {
85  if (IsBigEndian()) {
86  return x;
87  } else {
88  return ReverseBytes(x);
89  }
90 }
91 
92 template <typename T>
93 T NativeToLittleEndian(const T x) {
94  if (IsLittleEndian()) {
95  return x;
96  } else {
97  return ReverseBytes(x);
98  }
99 }
100 
101 template <typename T>
102 T NativeToBigEndian(const T x) {
103  if (IsBigEndian()) {
104  return x;
105  } else {
106  return ReverseBytes(x);
107  }
108 }
109 
110 template <typename T>
111 T ReadBinaryLittleEndian(std::istream* stream) {
112  T data_little_endian;
113  stream->read(reinterpret_cast<char*>(&data_little_endian), sizeof(T));
114  return LittleEndianToNative(data_little_endian);
115 }
116 
117 template <typename T>
118 void ReadBinaryLittleEndian(std::istream* stream, std::vector<T>* data) {
119  for (size_t i = 0; i < data->size(); ++i) {
120  (*data)[i] = ReadBinaryLittleEndian<T>(stream);
121  }
122 }
123 
124 template <typename T>
125 void WriteBinaryLittleEndian(std::ostream* stream, const T& data) {
126  const T data_little_endian = NativeToLittleEndian(data);
127  stream->write(reinterpret_cast<const char*>(&data_little_endian),
128  sizeof(T));
129 }
130 
131 template <typename T>
132 void WriteBinaryLittleEndian(std::ostream* stream, const std::vector<T>& data) {
133  for (const auto& elem : data) {
134  WriteBinaryLittleEndian<T>(stream, elem);
135  }
136 }
137 
138 } // namespace colmap
GraphType data
Definition: graph_cut.cc:138
normal_z x
T NativeToLittleEndian(const T x)
Definition: endian.h:93
bool IsLittleEndian()
Definition: endian.h:58
T NativeToBigEndian(const T x)
Definition: endian.h:102
bool IsBigEndian()
Definition: endian.h:66
T ReverseBytes(const T &data)
Definition: endian.h:51
T LittleEndianToNative(const T x)
Definition: endian.h:75
T ReadBinaryLittleEndian(std::istream *stream)
Definition: endian.h:111
T BigEndianToNative(const T x)
Definition: endian.h:84
void WriteBinaryLittleEndian(std::ostream *stream, const T &data)
Definition: endian.h:125