root/OpenSceneGraph/trunk/src/osgPlugins/txp/trpage_swap.cpp @ 11158

Revision 11158, 3.8 kB (checked in by robert, 5 years ago)

Fixed warning

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* ************************
2   Copyright Terrain Experts Inc.
3   Terrain Experts Inc (TERREX) reserves all rights to this source code
4   unless otherwise specified in writing by the President of TERREX.
5   This copyright may be updated in the future, in which case that version
6   supercedes this one.
7   -------------------
8   Terrex Experts Inc.
9   4400 East Broadway #314
10   Tucson, AZ  85711
11   info@terrex.com
12   Tel: (520) 323-7990
13   ************************
14   */
15
16#include <stdlib.h>
17#include <stdio.h>
18#include <string.h>
19#include <osg/Endian>
20
21/* trpage_swap.h
22    Byte swapping utility functions.
23    */
24
25#include <trpage_swap.h>
26/*
27** func:    swap_two( in, out )
28**
29** desc:    byte-swaps a two-byte array.
30*/
31void trpg_swap_two ( const char *in, char *out )
32{
33  char tmp[2];
34
35  tmp[0] = in[1] ;
36  tmp[1] = in[0] ;
37
38  memcpy(out,tmp,2);
39}
40
41/*
42** func:    swap_four( in, out )
43**
44** desc:    byte-swaps a four-byte array.
45*/
46void trpg_swap_four ( const char *in, char *out )
47{
48  char tmp[4];
49
50  tmp[0] = in[3] ;
51  tmp[1] = in[2] ;
52  tmp[2] = in[1] ;
53  tmp[3] = in[0] ;
54
55  memcpy(out,tmp,4);
56}
57
58/*
59** func:    swap_eight( in, out )
60**
61** desc:    byte-swaps an eight-byte array.
62*/
63void trpg_swap_eight ( const char *in, char *out )
64{
65  char tmp[8];
66
67  tmp[0] = in[7] ;
68  tmp[1] = in[6] ;
69  tmp[2] = in[5] ;
70  tmp[3] = in[4] ;
71  tmp[4] = in[3] ;
72  tmp[5] = in[2] ;
73  tmp[6] = in[1] ;
74  tmp[7] = in[0] ;
75
76  memcpy(out,tmp,8);
77}
78
79/*
80** func:    swap_sixteen( in, out )
81**
82** desc:    byte-swaps an sixteen-byte array.
83*/
84void trpg_swap_sixteen ( const char *in, char *out )
85{
86  char tmp[16];
87
88  tmp[0] = in[15] ;
89  tmp[1] = in[14] ;
90  tmp[2] = in[13] ;
91  tmp[3] = in[12] ;
92  tmp[4] = in[11] ;
93  tmp[5] = in[10] ;
94  tmp[6] = in[9] ;
95  tmp[7] = in[8] ;
96  tmp[8] = in[7] ;
97  tmp[9] = in[6] ;
98  tmp[10] = in[5] ;
99  tmp[11] = in[4] ;
100  tmp[12] = in[3] ;
101  tmp[13] = in[2] ;
102  tmp[14] = in[1] ;
103  tmp[15] = in[0] ;
104
105  memcpy(out,tmp,16);
106}
107
108/*
109** func:    tx_byteswap_short( number )
110**
111** desc:    byte-swaps a short int.
112*/
113short trpg_byteswap_short( short number )
114{
115    short result;
116
117    trpg_swap_two( (const char*) &number, (char*) &result );
118    return result;
119}
120
121/*
122** func:    tx_byteswap_int( number )
123**
124** desc:    byte-swaps an int.
125*/
126TX_CPPDECL int    trpg_byteswap_int( int number )
127{
128    int result;
129
130    trpg_swap_four( (const char*) &number, (char*) &result );
131    return result;
132}
133
134/*
135** func:    tx_byteswap_long( number )
136**
137** desc:    byte-swaps a long int.
138*/
139long trpg_byteswap_long( long number )
140{
141    long result;
142
143    trpg_swap_four( (const char*) &number, (char*) &result );
144    return result;
145}
146
147/*
148** func:    tx_byteswap_float( number )
149**
150** desc:    byte-swaps a float.
151*/
152void trpg_byteswap_float_to_4bytes( float number, char result[4] )
153{
154    trpg_swap_four( (const char*) &number, result );
155}
156
157/*
158** func:    tx_byteswap_double_to_8bytes( number )
159**
160** desc:    byte-swaps a double.
161*/
162void trpg_byteswap_double_to_8bytes( double number, char result[8] )
163{
164    trpg_swap_eight( (const char*) &number, result );
165}
166
167
168/*
169** func:    tx_byteswap_float( number )
170**
171** desc:    byte-swaps a float.
172*/
173float trpg_byteswap_4bytes_to_float( const char result[4] )
174{
175    float number;
176    trpg_swap_four( result, (char*) &number );
177    return number;
178}
179
180/*
181** func:    tx_byteswap_double_to_8bytes( number )
182**
183** desc:    byte-swaps a double.
184*/
185double trpg_byteswap_8bytes_to_double( const char result[8] )
186{
187    double number;
188    trpg_swap_eight( result, (char*) &number );
189    return number;
190}
191
192trpgllong trpg_byteswap_llong ( trpgllong number )
193{
194    trpgllong result;
195
196    trpg_swap_eight ( (char *) &number, (char *) &result);
197
198    return result;
199}
200
201TX_CPPDECL trpgEndian trpg_cpu_byte_order(void)
202{
203    if (osg::getCpuByteOrder()==osg::BigEndian)
204        return BigEndian;
205    else 
206        return LittleEndian;
207}
Note: See TracBrowser for help on using the browser.