1 /************************************************************************/ 2 /* */ 3 /* Copyright 2002 by Gunnar Kedenburg */ 4 /* */ 5 /* This file is part of the VIGRA computer vision library. */ 6 /* The VIGRA Website is */ 7 /* http://hci.iwr.uni-heidelberg.de/vigra/ */ 8 /* Please direct questions, bug reports, and contributions to */ 9 /* ullrich.koethe@iwr.uni-heidelberg.de or */ 10 /* vigra@informatik.uni-hamburg.de */ 11 /* */ 12 /* Permission is hereby granted, free of charge, to any person */ 13 /* obtaining a copy of this software and associated documentation */ 14 /* files (the "Software"), to deal in the Software without */ 15 /* restriction, including without limitation the rights to use, */ 16 /* copy, modify, merge, publish, distribute, sublicense, and/or */ 17 /* sell copies of the Software, and to permit persons to whom the */ 18 /* Software is furnished to do so, subject to the following */ 19 /* conditions: */ 20 /* */ 21 /* The above copyright notice and this permission notice shall be */ 22 /* included in all copies or substantial portions of the */ 23 /* Software. */ 24 /* */ 25 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND */ 26 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES */ 27 /* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */ 28 /* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */ 29 /* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */ 30 /* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */ 31 /* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR */ 32 /* OTHER DEALINGS IN THE SOFTWARE. */ 33 /* */ 34 /************************************************************************/ 35 36 #ifndef VIGRA_BYTEORDER_HXX 37 #define VIGRA_BYTEORDER_HXX 38 39 #include <vector> 40 #include <memory> 41 #include <string> 42 #include <fstream> 43 #include <algorithm> 44 #include "vigra/sized_int.hxx" 45 46 namespace vigra 47 { 48 class byteorder 49 { 50 class host 51 { 52 std::string m_string; 53 54 public: 55 56 // ctor, dtor 57 58 host(); 59 60 // methods 61 62 const std::string & get() const; 63 }; 64 65 // attributes 66 67 static const host m_host; 68 69 // data byte order, can be changed 70 71 std::string m_string; 72 bool native; 73 74 // delegation methods 75 76 template< class T > reversebytes(T & x) const77 void reversebytes( T & x ) const 78 { 79 const size_t n = sizeof(T); 80 UInt8 t[n]; 81 UInt8 * c = reinterpret_cast< UInt8 * >(&x); 82 size_t i; 83 for( i = 0; i < n; ++i ) 84 t[i] = c[ n - 1 - i ]; 85 for( i = 0; i < n; ++i ) 86 c[i] = t[i]; 87 } 88 89 public: 90 91 // ctor, dtor 92 93 byteorder(); // uses the host byteorder 94 byteorder( const std::string & ); 95 96 // methods 97 98 void set( const std::string & ); 99 const std::string & get() const; 100 const std::string & get_host_byteorder() const; 101 102 template< class T > convert_to_host(T & x) const103 void convert_to_host( T & x ) const 104 { 105 if (!native) 106 reversebytes(x); 107 } 108 109 template< class T > convert_to_host(T * x,size_t num) const110 void convert_to_host( T * x, size_t num ) const 111 { 112 if (!native) 113 for( size_t i = 0; i < num; ++i ) 114 reversebytes(x[i]); 115 } 116 117 template< class T > convert_from_host(T & x) const118 void convert_from_host( T & x ) const 119 { 120 if (!native) 121 reversebytes(x); 122 } 123 124 template< class T > convert_from_host(T * x,size_t num) const125 void convert_from_host( T * x, size_t num ) const 126 { 127 if (!native) 128 for( size_t i = 0; i < num; ++i ) 129 reversebytes(x[i]); 130 } 131 convert_to_host(char &) const132 void convert_to_host( char & ) const {} convert_to_host(Int8 &) const133 void convert_to_host( Int8 & ) const {} convert_to_host(UInt8 &) const134 void convert_to_host( UInt8 & ) const {} 135 convert_to_host(char *,size_t) const136 void convert_to_host( char * , size_t) const {} convert_to_host(Int8 *,size_t) const137 void convert_to_host( Int8 * , size_t) const {} convert_to_host(UInt8 *,size_t) const138 void convert_to_host( UInt8 * , size_t) const {} 139 convert_from_host(char &) const140 void convert_from_host( char & ) const {} convert_from_host(Int8 &) const141 void convert_from_host( Int8 & ) const {} convert_from_host(UInt8 &) const142 void convert_from_host( UInt8 & ) const {} 143 convert_from_host(char *,size_t) const144 void convert_from_host( char * , size_t) const {} convert_from_host(Int8 *,size_t) const145 void convert_from_host( Int8 * , size_t) const {} convert_from_host(UInt8 *,size_t) const146 void convert_from_host( UInt8 * , size_t) const {} 147 }; 148 149 template< class T > read_field(std::ifstream & stream,const byteorder & bo,T & x)150 void read_field( std::ifstream & stream, const byteorder & bo, T & x ) 151 { 152 stream.read( reinterpret_cast< char * >(&x), sizeof(T) ); 153 bo.convert_to_host(x); 154 } 155 156 template< class T > read_array(std::ifstream & stream,const byteorder & bo,T * x,size_t num)157 void read_array( std::ifstream & stream, const byteorder & bo, T * x, 158 size_t num ) 159 { 160 stream.read( reinterpret_cast< char * >(x), static_cast<std::streamsize>(sizeof(T) * num) ); 161 bo.convert_to_host( x, num ); 162 } 163 164 template< class T > write_field(std::ofstream & stream,const byteorder & bo,T t)165 void write_field( std::ofstream & stream, const byteorder & bo, T t ) 166 { 167 bo.convert_from_host(t); 168 stream.write( reinterpret_cast< char * >(&t), sizeof(T) ); 169 } 170 171 template< class T > write_array(std::ofstream & stream,const byteorder & bo,const T * x,size_t num)172 void write_array( std::ofstream & stream, const byteorder & bo, 173 const T * x, size_t num ) 174 { 175 for( size_t i = 0; i < num; ++i ) 176 write_field( stream, bo, x[i] ); 177 } 178 179 } // namespace vigra 180 181 #endif // VIGRA_BYTEORDER_HXX 182