Loading...
Searching...
No Matches
ByteSwapUtils.h
Go to the documentation of this file.
1/* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2
3 This file is part of the TerraLib - a Framework for building GIS enabled applications.
4
5 TerraLib is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 TerraLib is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with TerraLib. See COPYING. If not, write to
17 TerraLib Team at <terralib-team@terralib.org>.
18 */
19
20/*!
21 \file terralib/common/ByteSwapUtils.h
22
23 \brief Utility functions to swap bytes: double, int and unsigned int.
24
25 \ingroup common
26 */
27
28#ifndef __TERRALIB_COMMON_INTERNAL_BYTESWAPUTILS_H
29#define __TERRALIB_COMMON_INTERNAL_BYTESWAPUTILS_H
30
31// TerraLib
32#include "Config.h"
33
34// STL
35#include <cstring>
36
37// Boost
38#include <boost/cstdint.hpp>
39
40namespace te
41{
42 namespace common
43 {
44 /*!
45 \brief It swaps two bytes in local.
46
47 \param v The value we want to swap.
48
49 \note Requirements: The type T must have a sizeof(T) == 2.
50 */
51 template<class T> inline void Swap2Bytes(T& v)
52 {
53 char vIn[2], vOut[2];
54
55 memcpy(vIn, &v, sizeof(T));
56
57 vOut[0] = vIn[1];
58 vOut[1] = vIn[0];
59
60 memcpy(&v, vOut, sizeof(T));
61 }
62
63 /*!
64 \brief It swaps an array of two bytes in local.
65
66 \param v The value we want to swap.
67 */
68 inline void Swap2Bytes(char* v)
69 {
70 char v0 = v[0];
71 v[0] = v[1];
72 v[1] = v0;
73 }
74
75 /*!
76 \brief It swaps four bytes in local.
77
78 \param v The value we want to swap.
79
80 \note Requirements: The type T must have a sizeof(T) == 4.
81 */
82 template<class T> inline void Swap4Bytes(T& v)
83 {
84 char vIn[4], vOut[4];
85
86 memcpy(vIn, &v, sizeof(T));
87
88 vOut[0] = vIn[3];
89 vOut[1] = vIn[2];
90 vOut[2] = vIn[1];
91 vOut[3] = vIn[0];
92
93 memcpy(&v, vOut, sizeof(T));
94 }
95
96 /*!
97 \brief It swaps an array of four bytes in local.
98
99 \param v The value we want to swap.
100 */
101 inline void Swap4Bytes(char* v)
102 {
103 char v0 = v[0];
104 v[0] = v[3];
105 v[3] = v0;
106
107 char v1 = v[1];
108 v[1] = v[2];
109 v[2] = v1;
110 }
111
112 /*!
113 \brief It swaps the bytes in local.
114
115 \param v The value we want to swap.
116
117 \note Generic version: swap n-bytes.
118 */
119 template<class T> inline void SwapBytes(T& v)
120 {
121 char* vIn = new char[sizeof(T)];
122 char* vOut = new char[sizeof(T)];
123
124 memcpy(vIn, &v, sizeof(T));
125
126 for(unsigned int i = 0; i < sizeof(T); ++i)
127 vOut[i] = vIn[sizeof(T) - i - 1];
128
129 memcpy(&v, vOut, sizeof(T));
130 delete [] vIn;
131 delete [] vOut;
132 }
133
134 /*!
135 \brief It swaps an array of eight bytes in local.
136
137 \param v The value we want to swap.
138 */
139 inline void Swap8Bytes(char* v)
140 {
141 char v0 = v[0];
142 v[0] = v[7];
143 v[7] = v0;
144
145 char v1 = v[1];
146 v[1] = v[6];
147 v[6] = v1;
148
149 char v2 = v[2];
150 v[2] = v[5];
151 v[5] = v2;
152
153 char v3 = v[3];
154 v[3] = v[4];
155 v[4] = v3;
156 }
157
158 /*!
159 \brief It swaps 2 bytes in local.
160
161 \param v The value we want to swap.
162 */
163 template<> inline void SwapBytes(boost::int16_t& v)
164 {
165 Swap2Bytes(v);
166 }
167
168 /*!
169 \brief It swaps four bytes in local.
170
171 \param v The value we want to swap.
172 */
173 template<> inline void SwapBytes(boost::int32_t& v)
174 {
175 Swap4Bytes(v);
176 }
177
178 /*!
179 \brief It swaps four bytes in local.
180
181 \param v The value we want to swap.
182 */
183 template<> inline void SwapBytes(boost::uint32_t& v)
184 {
185 Swap4Bytes(v);
186 }
187
188 /*!
189 \brief It swaps four bytes in local.
190
191 \param v The value we want to swap.
192 */
193 template<> inline void SwapBytes(float& v)
194 {
195 Swap4Bytes(v);
196 }
197
198 /*!
199 \brief It swaps eight bytes in local.
200
201 \param v The value we want to swap.
202 */
203 template<> inline void SwapBytes(double& v)
204 {
205 /*!
206 \union SwapDoubleUnion
207
208 \brief This union is meant to be used only by byte swap functions.
209
210 \note This struct is for internal use only. Don't use it in your own code.
211 */
212 union SwapDoubleUnion
213 {
214 double m_dWord; //!< Double word (64 bit).
215 unsigned int m_uintWord[2]; //!< Unsigned int word (64 bit).
216 };
217
218 SwapDoubleUnion vAux1, vAux2;
219
220 vAux1.m_dWord = v;
221
222 SwapBytes(vAux1.m_uintWord[0]);
223 SwapBytes(vAux1.m_uintWord[1]);
224
225 vAux2.m_uintWord[1] = vAux1.m_uintWord[0];
226 vAux2.m_uintWord[0] = vAux1.m_uintWord[1];
227
228 v = vAux2.m_dWord;
229 }
230
231 } // end namespace common
232} // end namespace te
233
234#endif // __TERRALIB_COMMON_INTERNAL_BYTESWAPUTILS_H
235
void Swap4Bytes(T &v)
It swaps four bytes in local.
Definition: ByteSwapUtils.h:82
void SwapBytes(T &v)
It swaps the bytes in local.
void Swap8Bytes(char *v)
It swaps an array of eight bytes in local.
void Swap2Bytes(T &v)
It swaps two bytes in local.
Definition: ByteSwapUtils.h:51
TerraLib.
Proxy configuration file for TerraView (see terraview_config.h).
It swaps eight bytes in local.