OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimEndian.h
Go to the documentation of this file.
1 //******************************************************************
2 //
3 // License: See top level LICENSE.txt file.
4 //
5 // Author: Garrett Potts
6 //
7 // Description: This file contains the interface to the
8 // endian byte swap
9 //
10 //***********************************
11 // $ID$
12 #ifndef ossimEndian_HEADER
13 #define ossimEndian_HEADER
14 
15 // for OSSIM_LITTLE_ENDIAN AND BIG_ENDIAN
17 
19 {
20 public:
21  ossimEndian();
22 
26  inline void swap(ossim_sint8 &/*data*/){}
27  inline void swap(ossim_uint8 &/*data*/){}
28  inline void swap(ossim_int16 &data) const;
29  inline void swap(ossim_uint16 &data) const;
30  inline void swap(ossim_int32 &data) const;
31  inline void swap(ossim_uint32 &data) const;
32  inline void swap(ossim_uint64 &data) const;
33  inline void swap(ossim_sint64 &data) const;
34  inline void swap(ossim_float32 &data) const;
35  inline void swap(ossim_float64 &data) const;
36 
42  inline void swap(ossimScalarType scalar, void* data, ossim_uint32 size) const;
43  // only here to allow template based swaps to compile correctly
44  //
45  inline void swap(ossim_sint8* data, ossim_uint32 size)const;
46  inline void swap(ossim_uint8* data, ossim_uint32 size)const;
47 
48  inline void swap(ossim_int16* data, ossim_uint32 size) const;
49  inline void swap(ossim_uint16* data, ossim_uint32 size) const;
50 
51  inline void swap(ossim_int32* data, ossim_uint32 size) const;
52  inline void swap(ossim_uint32* data, ossim_uint32 size) const;
53 
54  inline void swap(ossim_int64* data, ossim_uint32 size) const;
55  inline void swap(ossim_uint64* data, ossim_uint32 size) const;
56 
57  inline void swap(ossim_float32* data, ossim_uint32 size) const;
58  inline void swap(ossim_float64* data, ossim_uint32 size) const;
59 
60  inline void swapTwoBytes(void* data, ossim_uint32 size) const;
61  inline void swapFourBytes(void* data, ossim_uint32 size) const;
62  inline void swapEightBytes(void* data, ossim_uint32 size) const;
63  inline ossimByteOrder getSystemEndianType() const;
64 
65 private:
66 
67  // Holds the Endian of the architecture that you are running on.
69 
70  void swapTwoBytes(void *data) const;
71  void swapFourBytes(void *data) const;
72  void swapEightBytes(void *data) const;
73 
74  void swapPrivate(ossim_uint8 *c1,
75  ossim_uint8 *c2) const;
76 };
77 
79 {
80  return theSystemEndianType;
81 }
82 
83 inline void ossimEndian::swap(ossim_sint8* /* data */,
84  ossim_uint32 /* size */ )const
85 {
86  //intentionally left blank
87 }
88 
89 inline void ossimEndian::swap(ossim_uint8* /* data */,
90  ossim_uint32 /* size */ )const
91 {
92  //intentionally left blank
93 }
94 
95 inline void ossimEndian::swap(ossim_int16 &data) const
96 {
97  swapTwoBytes(reinterpret_cast<void*>(&data));
98 }
99 
100 inline void ossimEndian::swap(ossim_uint16 &data) const
101 {
102  swapTwoBytes(reinterpret_cast<void*>(&data));
103 }
104 
105 inline void ossimEndian::swap(ossim_int32 &data) const
106 {
107  swapFourBytes(reinterpret_cast<void*>(&data));
108 }
109 
110 inline void ossimEndian::swap(ossim_uint32 &data) const
111 {
112  swapFourBytes(reinterpret_cast<void*>(&data));
113 }
114 
115 inline void ossimEndian::swap(ossim_uint64 &data) const
116 {
117  swapEightBytes(reinterpret_cast<void*>(&data));
118 }
119 
120 inline void ossimEndian::swap(ossim_sint64 &data) const
121 {
122  swapEightBytes(reinterpret_cast<void*>(&data));
123 }
124 
125 inline void ossimEndian::swap(ossim_float32 &data) const
126 {
127  swapFourBytes(reinterpret_cast<void*>(&data));
128 }
129 
130 inline void ossimEndian::swap(ossim_float64 &data) const
131 {
132  swapEightBytes(reinterpret_cast<void*>(&data));
133 }
134 
135 inline void ossimEndian::swapTwoBytes(void *data) const
136 {
137  unsigned char *c = reinterpret_cast<unsigned char*>(data);
138 
139  swapPrivate(&c[0], &c[1]);
140 }
141 
142 inline void ossimEndian::swapFourBytes(void* data) const
143 {
144  unsigned char *c = reinterpret_cast<unsigned char*>(data);
145 
146  swapPrivate(&c[0], &c[3]);
147  swapPrivate(&c[1], &c[2]);
148 }
149 
150 inline void ossimEndian::swapEightBytes(void* data) const
151 {
152  unsigned char *c = reinterpret_cast<unsigned char*>(data);
153 
154  swapPrivate(&c[0], &c[7]);
155  swapPrivate(&c[1], &c[6]);
156  swapPrivate(&c[2], &c[5]);
157  swapPrivate(&c[3], &c[4]);
158 }
159 
161  ossim_uint8 *c2) const
162 {
163  ossim_uint8 temp_c = *c1;
164  *c1 = *c2;
165  *c2 = temp_c;
166 }
167 
169  void* data, ossim_uint32 size) const
170 {
171  switch (scalar)
172  {
173  case OSSIM_USHORT16:
174  case OSSIM_SSHORT16:
175  case OSSIM_USHORT11:
176  case OSSIM_USHORT12:
177  case OSSIM_USHORT13:
178  case OSSIM_USHORT14:
179  case OSSIM_USHORT15:
180  swapTwoBytes(data, size);
181  return;
182 
183  case OSSIM_FLOAT:
185  swapFourBytes(data, size);
186  return;
187 
188  case OSSIM_DOUBLE:
190  swapEightBytes(data, size);
191  break;
192 
193  default:
194  return;
195  }
196 }
197 
199 {
200  swapTwoBytes(data, size);
201 }
202 
204 {
205  swapTwoBytes(data, size);
206 }
207 
209 {
210  swapFourBytes(data, size);
211 }
212 
214 {
215  swapFourBytes(data, size);
216 }
217 
219 {
220  swapEightBytes(data, size);
221 }
222 
224 {
225  swapEightBytes(data, size);
226 }
227 
229 {
230  swapFourBytes(data, size);
231 }
232 
234 {
235  swapEightBytes(data, size);
236 }
237 
238 inline void ossimEndian::swapTwoBytes(void* data, ossim_uint32 size) const
239 {
240  ossim_uint16* buf = reinterpret_cast<ossim_uint16*>(data);
241  for (ossim_uint32 i=0; i<size; ++i)
242  {
243  buf[i] = ((buf[i] & 0x00ff) << 8) | ((buf[i] & 0xff00) >> 8);
244  }
245 }
246 
247 inline void ossimEndian::swapFourBytes(void* data, ossim_uint32 size) const
248 {
249  ossim_uint32* buf = reinterpret_cast<ossim_uint32*>(data);
250  for (ossim_uint32 i=0; i<size; ++i)
251  {
252  buf[i]
253  = ( ((buf[i] & 0xff000000) >> 24)
254  | ((buf[i] & 0x00ff0000) >> 8)
255  | ((buf[i] & 0x0000ff00) << 8)
256  | ((buf[i] & 0x000000ff) << 24));
257  }
258 }
259 
260 inline void ossimEndian::swapEightBytes(void* data, ossim_uint32 size) const
261 {
262  ossim_uint64* buf = reinterpret_cast<ossim_uint64*>(data);
263  for (ossim_uint32 i=0; i<size; ++i)
264  {
265  buf[i]
266  = ( ((buf[i] & 0xff00000000000000ull) >> 56)
267  | ((buf[i] & 0x00ff000000000000ull) >> 40)
268  | ((buf[i] & 0x0000ff0000000000ull) >> 24)
269  | ((buf[i] & 0x000000ff00000000ull) >> 8)
270  | ((buf[i] & 0x00000000ff000000ull) << 8)
271  | ((buf[i] & 0x0000000000ff0000ull) << 24)
272  | ((buf[i] & 0x000000000000ff00ull) << 40)
273  | ((buf[i] & 0x00000000000000ffull) << 56));
274  }
275 }
276 
277 #endif /* End of #ifndef ossimEndian_HEADER */
16 bit unsigned integer (15 bits used)
void swapFourBytes(void *data, ossim_uint32 size) const
Definition: ossimEndian.h:247
void swapTwoBytes(void *data, ossim_uint32 size) const
Definition: ossimEndian.h:238
void swapEightBytes(void *data, ossim_uint32 size) const
Definition: ossimEndian.h:260
#define OSSIMDLLEXPORT
float ossim_float32
void swapPrivate(ossim_uint8 *c1, ossim_uint8 *c2) const
Definition: ossimEndian.h:160
16 bit unsigned integer (14 bits used)
signed char ossim_sint8
16 bit unsigned integer (13 bits used)
unsigned short ossim_uint16
double ossim_float64
yy_size_t size
unsigned long long ossim_uint64
unsigned int ossim_uint32
32 bit normalized floating point
ossimByteOrder
ossimByteOrder getSystemEndianType() const
Definition: ossimEndian.h:78
ossimByteOrder theSystemEndianType
Definition: ossimEndian.h:68
ossimScalarType
64 bit normalized floating point
16 bit unsigned integer (11 bits used)
short ossim_int16
long long ossim_int64
void swap(ossim_uint8 &)
Definition: ossimEndian.h:27
32 bit floating point
void swap(ossim_sint8 &)
Definition: ossimEndian.h:26
16 bit unsigned iteger
64 bit floating point
16 bit signed integer
unsigned char ossim_uint8
signed long long ossim_sint64
int ossim_int32
16 bit unsigned integer (12 bits used)