Developer Documentation
Loading...
Searching...
No Matches
BinaryHelper.cc
1/* ========================================================================= *
2 * *
3 * OpenMesh *
4 * Copyright (c) 2001-2025, 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
45//=============================================================================
46//
47// Helper Functions for binary reading / writing
48//
49//=============================================================================
50
51
52//== INCLUDES =================================================================
53
54
55// -------------------- STL
56#include <algorithm>
57#include <iostream>
58// -------------------- OpenMesh
59#include <OpenMesh/Core/IO/BinaryHelper.hh>
60
61
62//== NAMESPACES ===============================================================
63
64
65namespace OpenMesh {
66namespace IO {
67
68#ifndef DOXY_IGNORE_THIS
69
70//== IMPLEMENTATION ===========================================================
71
72//-----------------------------------------------------------------------------
73
74short int read_short(FILE* _in, bool _swap)
75{
76 union u1 { short int s; unsigned char c[2]; } sc;
77 fread(reinterpret_cast<char*>(sc.c), 1, 2, _in);
78 if (_swap) std::swap(sc.c[0], sc.c[1]);
79 return sc.s;
80}
81
82
83//-----------------------------------------------------------------------------
84
85
86int read_int(FILE* _in, bool _swap)
87{
88 union u2 { int i; unsigned char c[4]; } ic;
89 fread(reinterpret_cast<char*>(ic.c), 1, 4, _in);
90 if (_swap) {
91 std::swap(ic.c[0], ic.c[3]);
92 std::swap(ic.c[1], ic.c[2]);
93 }
94 return ic.i;
95}
96
97
98//-----------------------------------------------------------------------------
99
100
101float read_float(FILE* _in, bool _swap)
102{
103 union u3 { float f; unsigned char c[4]; } fc;
104 fread(reinterpret_cast<char*>(fc.c), 1, 4, _in);
105 if (_swap) {
106 std::swap(fc.c[0], fc.c[3]);
107 std::swap(fc.c[1], fc.c[2]);
108 }
109 return fc.f;
110}
111
112
113//-----------------------------------------------------------------------------
114
115
116double read_double(FILE* _in, bool _swap)
117{
118 union u4 { double d; unsigned char c[8]; } dc;
119 fread(reinterpret_cast<char*>(dc.c), 1, 8, _in);
120 if (_swap) {
121 std::swap(dc.c[0], dc.c[7]);
122 std::swap(dc.c[1], dc.c[6]);
123 std::swap(dc.c[2], dc.c[5]);
124 std::swap(dc.c[3], dc.c[4]);
125 }
126 return dc.d;
127}
128
129//-----------------------------------------------------------------------------
130
131short int read_short(std::istream& _in, bool _swap)
132{
133 union u1 { short int s; unsigned char c[2]; } sc;
134 _in.read(reinterpret_cast<char*>(sc.c), 2);
135 if (_swap) std::swap(sc.c[0], sc.c[1]);
136 return sc.s;
137}
138
139
140//-----------------------------------------------------------------------------
141
142
143int read_int(std::istream& _in, bool _swap)
144{
145 union u2 { int i; unsigned char c[4]; } ic;
146 _in.read(reinterpret_cast<char*>(ic.c), 4);
147 if (_swap) {
148 std::swap(ic.c[0], ic.c[3]);
149 std::swap(ic.c[1], ic.c[2]);
150 }
151 return ic.i;
152}
153
154
155//-----------------------------------------------------------------------------
156
157
158float read_float(std::istream& _in, bool _swap)
159{
160 union u3 { float f; unsigned char c[4]; } fc;
161 _in.read(reinterpret_cast<char*>(fc.c), 4);
162 if (_swap) {
163 std::swap(fc.c[0], fc.c[3]);
164 std::swap(fc.c[1], fc.c[2]);
165 }
166 return fc.f;
167}
168
169
170//-----------------------------------------------------------------------------
171
172
173double read_double(std::istream& _in, bool _swap)
174{
175 union u4 { double d; unsigned char c[8]; } dc;
176 _in.read(reinterpret_cast<char*>(dc.c), 8);
177 if (_swap) {
178 std::swap(dc.c[0], dc.c[7]);
179 std::swap(dc.c[1], dc.c[6]);
180 std::swap(dc.c[2], dc.c[5]);
181 std::swap(dc.c[3], dc.c[4]);
182 }
183 return dc.d;
184}
185
186
187//-----------------------------------------------------------------------------
188
189
190void write_short(short int _i, FILE* _out, bool _swap)
191{
192 union u1 { short int s; unsigned char c[2]; } sc;
193 sc.s = _i;
194 if (_swap) std::swap(sc.c[0], sc.c[1]);
195 fwrite(reinterpret_cast<char*>(sc.c), 1, 2, _out);
196}
197
198
199//-----------------------------------------------------------------------------
200
201
202void write_int(int _i, FILE* _out, bool _swap)
203{
204 union u2 { int i; unsigned char c[4]; } ic;
205 ic.i = _i;
206 if (_swap) {
207 std::swap(ic.c[0], ic.c[3]);
208 std::swap(ic.c[1], ic.c[2]);
209 }
210 fwrite(reinterpret_cast<char*>(ic.c), 1, 4, _out);
211}
212
213
214//-----------------------------------------------------------------------------
215
216
217void write_float(float _f, FILE* _out, bool _swap)
218{
219 union u3 { float f; unsigned char c[4]; } fc;
220 fc.f = _f;
221 if (_swap) {
222 std::swap(fc.c[0], fc.c[3]);
223 std::swap(fc.c[1], fc.c[2]);
224 }
225 fwrite(reinterpret_cast<char*>(fc.c), 1, 4, _out);
226}
227
228
229//-----------------------------------------------------------------------------
230
231
232void write_double(double _d, FILE* _out, bool _swap)
233{
234 union u4 { double d; unsigned char c[8]; } dc;
235 dc.d = _d;
236 if (_swap) {
237 std::swap(dc.c[0], dc.c[7]);
238 std::swap(dc.c[1], dc.c[6]);
239 std::swap(dc.c[2], dc.c[5]);
240 std::swap(dc.c[3], dc.c[4]);
241 }
242 fwrite(reinterpret_cast<char*>(dc.c), 1, 8, _out);
243}
244
245
246//-----------------------------------------------------------------------------
247
248
249void write_short(short int _i, std::ostream& _out, bool _swap)
250{
251 union u1 { short int s; unsigned char c[2]; } sc;
252 sc.s = _i;
253 if (_swap) std::swap(sc.c[0], sc.c[1]);
254 _out.write(reinterpret_cast<char*>(sc.c), 2);
255}
256
257
258//-----------------------------------------------------------------------------
259
260
261void write_int(int _i, std::ostream& _out, bool _swap)
262{
263 union u2 { int i; unsigned char c[4]; } ic;
264 ic.i = _i;
265 if (_swap) {
266 std::swap(ic.c[0], ic.c[3]);
267 std::swap(ic.c[1], ic.c[2]);
268 }
269 _out.write(reinterpret_cast<char*>(ic.c), 4);
270}
271
272
273//-----------------------------------------------------------------------------
274
275
276void write_float(float _f, std::ostream& _out, bool _swap)
277{
278 union u3 { float f; unsigned char c[4]; } fc;
279 fc.f = _f;
280 if (_swap) {
281 std::swap(fc.c[0], fc.c[3]);
282 std::swap(fc.c[1], fc.c[2]);
283 }
284 _out.write(reinterpret_cast<char*>(fc.c), 4);
285}
286
287
288//-----------------------------------------------------------------------------
289
290
291void write_double(double _d, std::ostream& _out, bool _swap)
292{
293 union u4 { double d; unsigned char c[8]; } dc;
294 dc.d = _d;
295 if (_swap) {
296 std::swap(dc.c[0], dc.c[7]);
297 std::swap(dc.c[1], dc.c[6]);
298 std::swap(dc.c[2], dc.c[5]);
299 std::swap(dc.c[3], dc.c[4]);
300 }
301 _out.write(reinterpret_cast<char*>(dc.c), 8);
302}
303
304
305#endif
306
307//=============================================================================
308} // namespace IO
309} // namespace OpenMesh
310//=============================================================================
short int read_short(FILE *_in, bool _swap=false)
float read_float(FILE *_in, bool _swap=false)
void write_int(int _i, FILE *_out, bool _swap=false)
void write_double(double _d, FILE *_out, bool _swap=false)
void write_short(short int _i, FILE *_out, bool _swap=false)
int read_int(FILE *_in, bool _swap=false)
double read_double(FILE *_in, bool _swap=false)
void write_float(float _f, FILE *_out, bool _swap=false)