Developer Documentation
BinaryHelper.cc
1 /* ========================================================================= *
2  * *
3  * OpenMesh *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openmesh.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenMesh. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39  * *
40  * ========================================================================= */
41 
42 /*===========================================================================*\
43  * *
44  * $Revision$ *
45  * $Date$ *
46  * *
47 \*===========================================================================*/
48 
49 
50 //=============================================================================
51 //
52 // Helper Functions for binary reading / writing
53 //
54 //=============================================================================
55 
56 
57 //== INCLUDES =================================================================
58 
59 
60 // -------------------- STL
61 #include <algorithm>
62 #include <iostream>
63 // -------------------- OpenMesh
64 #include <OpenMesh/Core/IO/BinaryHelper.hh>
65 
66 
67 //== NAMESPACES ===============================================================
68 
69 
70 namespace OpenMesh {
71 namespace IO {
72 
73 #ifndef DOXY_IGNORE_THIS
74 
75 //== IMPLEMENTATION ===========================================================
76 
77 //-----------------------------------------------------------------------------
78 
79 short int read_short(FILE* _in, bool _swap)
80 {
81  union u1 { short int s; unsigned char c[2]; } sc;
82  fread((char*)sc.c, 1, 2, _in);
83  if (_swap) std::swap(sc.c[0], sc.c[1]);
84  return sc.s;
85 }
86 
87 
88 //-----------------------------------------------------------------------------
89 
90 
91 int read_int(FILE* _in, bool _swap)
92 {
93  union u2 { int i; unsigned char c[4]; } ic;
94  fread((char*)ic.c, 1, 4, _in);
95  if (_swap) {
96  std::swap(ic.c[0], ic.c[3]);
97  std::swap(ic.c[1], ic.c[2]);
98  }
99  return ic.i;
100 }
101 
102 
103 //-----------------------------------------------------------------------------
104 
105 
106 float read_float(FILE* _in, bool _swap)
107 {
108  union u3 { float f; unsigned char c[4]; } fc;
109  fread((char*)fc.c, 1, 4, _in);
110  if (_swap) {
111  std::swap(fc.c[0], fc.c[3]);
112  std::swap(fc.c[1], fc.c[2]);
113  }
114  return fc.f;
115 }
116 
117 
118 //-----------------------------------------------------------------------------
119 
120 
121 double read_double(FILE* _in, bool _swap)
122 {
123  union u4 { double d; unsigned char c[8]; } dc;
124  fread((char*)dc.c, 1, 8, _in);
125  if (_swap) {
126  std::swap(dc.c[0], dc.c[7]);
127  std::swap(dc.c[1], dc.c[6]);
128  std::swap(dc.c[2], dc.c[5]);
129  std::swap(dc.c[3], dc.c[4]);
130  }
131  return dc.d;
132 }
133 
134 //-----------------------------------------------------------------------------
135 
136 short int read_short(std::istream& _in, bool _swap)
137 {
138  union u1 { short int s; unsigned char c[2]; } sc;
139  _in.read((char*)sc.c, 2);
140  if (_swap) std::swap(sc.c[0], sc.c[1]);
141  return sc.s;
142 }
143 
144 
145 //-----------------------------------------------------------------------------
146 
147 
148 int read_int(std::istream& _in, bool _swap)
149 {
150  union u2 { int i; unsigned char c[4]; } ic;
151  _in.read((char*)ic.c, 4);
152  if (_swap) {
153  std::swap(ic.c[0], ic.c[3]);
154  std::swap(ic.c[1], ic.c[2]);
155  }
156  return ic.i;
157 }
158 
159 
160 //-----------------------------------------------------------------------------
161 
162 
163 float read_float(std::istream& _in, bool _swap)
164 {
165  union u3 { float f; unsigned char c[4]; } fc;
166  _in.read((char*)fc.c, 4);
167  if (_swap) {
168  std::swap(fc.c[0], fc.c[3]);
169  std::swap(fc.c[1], fc.c[2]);
170  }
171  return fc.f;
172 }
173 
174 
175 //-----------------------------------------------------------------------------
176 
177 
178 double read_double(std::istream& _in, bool _swap)
179 {
180  union u4 { double d; unsigned char c[8]; } dc;
181  _in.read((char*)dc.c, 8);
182  if (_swap) {
183  std::swap(dc.c[0], dc.c[7]);
184  std::swap(dc.c[1], dc.c[6]);
185  std::swap(dc.c[2], dc.c[5]);
186  std::swap(dc.c[3], dc.c[4]);
187  }
188  return dc.d;
189 }
190 
191 
192 //-----------------------------------------------------------------------------
193 
194 
195 void write_short(short int _i, FILE* _out, bool _swap)
196 {
197  union u1 { short int s; unsigned char c[2]; } sc;
198  sc.s = _i;
199  if (_swap) std::swap(sc.c[0], sc.c[1]);
200  fwrite((char*)sc.c, 1, 2, _out);
201 }
202 
203 
204 //-----------------------------------------------------------------------------
205 
206 
207 void write_int(int _i, FILE* _out, bool _swap)
208 {
209  union u2 { int i; unsigned char c[4]; } ic;
210  ic.i = _i;
211  if (_swap) {
212  std::swap(ic.c[0], ic.c[3]);
213  std::swap(ic.c[1], ic.c[2]);
214  }
215  fwrite((char*)ic.c, 1, 4, _out);
216 }
217 
218 
219 //-----------------------------------------------------------------------------
220 
221 
222 void write_float(float _f, FILE* _out, bool _swap)
223 {
224  union u3 { float f; unsigned char c[4]; } fc;
225  fc.f = _f;
226  if (_swap) {
227  std::swap(fc.c[0], fc.c[3]);
228  std::swap(fc.c[1], fc.c[2]);
229  }
230  fwrite((char*)fc.c, 1, 4, _out);
231 }
232 
233 
234 //-----------------------------------------------------------------------------
235 
236 
237 void write_double(double _d, FILE* _out, bool _swap)
238 {
239  union u4 { double d; unsigned char c[8]; } dc;
240  dc.d = _d;
241  if (_swap) {
242  std::swap(dc.c[0], dc.c[7]);
243  std::swap(dc.c[1], dc.c[6]);
244  std::swap(dc.c[2], dc.c[5]);
245  std::swap(dc.c[3], dc.c[4]);
246  }
247  fwrite((char*)dc.c, 1, 8, _out);
248 }
249 
250 
251 //-----------------------------------------------------------------------------
252 
253 
254 void write_short(short int _i, std::ostream& _out, bool _swap)
255 {
256  union u1 { short int s; unsigned char c[2]; } sc;
257  sc.s = _i;
258  if (_swap) std::swap(sc.c[0], sc.c[1]);
259  _out.write((char*)sc.c, 2);
260 }
261 
262 
263 //-----------------------------------------------------------------------------
264 
265 
266 void write_int(int _i, std::ostream& _out, bool _swap)
267 {
268  union u2 { int i; unsigned char c[4]; } ic;
269  ic.i = _i;
270  if (_swap) {
271  std::swap(ic.c[0], ic.c[3]);
272  std::swap(ic.c[1], ic.c[2]);
273  }
274  _out.write((char*)ic.c, 4);
275 }
276 
277 
278 //-----------------------------------------------------------------------------
279 
280 
281 void write_float(float _f, std::ostream& _out, bool _swap)
282 {
283  union u3 { float f; unsigned char c[4]; } fc;
284  fc.f = _f;
285  if (_swap) {
286  std::swap(fc.c[0], fc.c[3]);
287  std::swap(fc.c[1], fc.c[2]);
288  }
289  _out.write((char*)fc.c, 4);
290 }
291 
292 
293 //-----------------------------------------------------------------------------
294 
295 
296 void write_double(double _d, std::ostream& _out, bool _swap)
297 {
298  union u4 { double d; unsigned char c[8]; } dc;
299  dc.d = _d;
300  if (_swap) {
301  std::swap(dc.c[0], dc.c[7]);
302  std::swap(dc.c[1], dc.c[6]);
303  std::swap(dc.c[2], dc.c[5]);
304  std::swap(dc.c[3], dc.c[4]);
305  }
306  _out.write((char*)dc.c, 8);
307 }
308 
309 
310 #endif
311 
312 //=============================================================================
313 } // namespace IO
314 } // namespace OpenMesh
315 //=============================================================================
short int read_short(FILE *_in, bool _swap=false)
void write_int(int _i, FILE *_out, bool _swap=false)
void write_float(float _f, FILE *_out, bool _swap=false)
double read_double(FILE *_in, bool _swap=false)
void write_double(double _d, FILE *_out, bool _swap=false)
float read_float(FILE *_in, bool _swap=false)
int read_int(FILE *_in, bool _swap=false)
void write_short(short int _i, FILE *_out, bool _swap=false)