1 2 /**************************************************************************** 3 4 Functions for reading basic data types from LWO2 files 5 6 Copyright (C) 2002 by Marco Jez 7 8 ****************************************************************************/ 9 10 #ifndef LWO2READ_ 11 #define LWO2READ_ 12 13 #include "lwo2types.h" 14 15 namespace lwo2 16 { 17 18 template<class Iter> read_I1(Iter & it)19I1 read_I1(Iter &it) 20 { 21 return static_cast<I1>(*(it++)); 22 } 23 24 template<class Iter> read_I2(Iter & it)25I2 read_I2(Iter &it) 26 { 27 I2 i2(((static_cast<I2>(*(it)) & 0xFF) << 8) | 28 (static_cast<I2>(*(it+1)) & 0xFF)); 29 it += 2; 30 return i2; 31 } 32 33 template<class Iter> read_I4(Iter & it)34I4 read_I4(Iter &it) 35 { 36 I4 i4(((static_cast<I4>(*(it)) & 0xFF) << 24) | 37 ((static_cast<I4>(*(it+1)) & 0xFF) << 16) | 38 ((static_cast<I4>(*(it+2)) & 0xFF) << 8) | 39 (static_cast<I4>(*(it+3)) & 0xFF)); 40 it += 4; 41 return i4; 42 } 43 44 template<class Iter> read_U1(Iter & it)45U1 read_U1(Iter &it) 46 { 47 return static_cast<U1>(*(it++)); 48 } 49 50 template<class Iter> read_U2(Iter & it)51U2 read_U2(Iter &it) 52 { 53 U2 u2(((static_cast<U2>(*(it)) & 0xFF) << 8) | 54 (static_cast<U2>(*(it+1)) & 0xFF)); 55 it += 2; 56 return u2; 57 } 58 59 template<class Iter> read_U4(Iter & it)60U4 read_U4(Iter &it) 61 { 62 U4 u4(((static_cast<U4>(*(it)) & 0xFF) << 24) | 63 ((static_cast<U4>(*(it+1)) & 0xFF) << 16) | 64 ((static_cast<U4>(*(it+2)) & 0xFF) << 8) | 65 (static_cast<U4>(*(it+3)) & 0xFF)); 66 it += 4; 67 return u4; 68 } 69 70 template<typename D, typename S> changeType4(S src)71D changeType4(S src) 72 { 73 D dest; 74 char* dest_ptr = reinterpret_cast<char*>(&dest); 75 char* src_ptr = reinterpret_cast<char*>(&src); 76 for(int i=0; i<4; ++i) 77 { 78 dest_ptr[i] = src_ptr[i]; 79 } 80 return dest; 81 } 82 83 template<class Iter> read_F4(Iter & it)84F4 read_F4(Iter &it) 85 { 86 return changeType4<F4, U4>(read_U4(it)); 87 } 88 89 template<class Iter> read_ID4(Iter & it)90ID4 read_ID4(Iter &it) 91 { 92 ID4 value; 93 for (int i=0; i<4; ++i) value.id[i] = *(it++); 94 return value; 95 } 96 97 template<class Iter> read_S0(Iter & it)98S0 read_S0(Iter &it) 99 { 100 S0 value; 101 while (*it) { 102 value += *(it++); 103 } 104 ++it; 105 if (value.length() % 2 == 0) ++it; 106 return value; 107 } 108 109 template<class Iter> read_VX(Iter & it)110VX read_VX(Iter &it) 111 { 112 VX vx; 113 if ((*it & 0xFF) == 0xFF) { 114 vx.index = read_U4(it) & 0x00FFFFFF; 115 } else { 116 vx.index = static_cast<U4>(read_U2(it)); 117 } 118 return vx; 119 } 120 121 template<class Iter> read_COL12(Iter & it)122COL12 read_COL12(Iter &it) 123 { 124 COL12 value; 125 value.red = read_F4(it); 126 value.green = read_F4(it); 127 value.blue = read_F4(it); 128 return value; 129 } 130 131 template<class Iter> read_VEC12(Iter & it)132VEC12 read_VEC12(Iter &it) 133 { 134 VEC12 value; 135 value.X = read_F4(it); 136 value.Y = read_F4(it); 137 value.Z = read_F4(it); 138 return value; 139 } 140 141 template<class Iter> read_FP4(Iter & it)142FP4 read_FP4(Iter &it) 143 { 144 FP4 value; 145 value.fraction = read_F4(it); 146 return value; 147 } 148 149 template<class Iter> read_ANG4(Iter & it)150ANG4 read_ANG4(Iter &it) 151 { 152 ANG4 value; 153 value.radians = read_F4(it); 154 return value; 155 } 156 157 template<class Iter> read_FNAM0(Iter & it)158FNAM0 read_FNAM0(Iter &it) 159 { 160 FNAM0 value; 161 value.name = read_S0(it); 162 return value; 163 } 164 165 } 166 167 #endif 168