| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | #include <osg/Notify> |
|---|
| 16 | #include <osg/ImageSequence> |
|---|
| 17 | #include <osgDB/ReadFile> |
|---|
| 18 | #include <osgDB/XmlParser> |
|---|
| 19 | #include <osgDB/FileNameUtils> |
|---|
| 20 | #include <osgDB/ObjectWrapper> |
|---|
| 21 | |
|---|
| 22 | using namespace osgDB; |
|---|
| 23 | |
|---|
| 24 | static std::string s_lastSchema; |
|---|
| 25 | |
|---|
| 26 | InputStream::InputStream( const osgDB::Options* options ) |
|---|
| 27 | : _fileVersion(0), _useSchemaData(false), _forceReadingImage(false), _dataDecompress(0) |
|---|
| 28 | { |
|---|
| 29 | if ( !options ) return; |
|---|
| 30 | _options = options; |
|---|
| 31 | |
|---|
| 32 | std::string schema; |
|---|
| 33 | if ( options->getPluginStringData("ForceReadingImage")=="true" ) |
|---|
| 34 | _forceReadingImage = true; |
|---|
| 35 | if ( !options->getPluginStringData("SchemaFile").empty() ) |
|---|
| 36 | { |
|---|
| 37 | schema = options->getPluginStringData("SchemaFile"); |
|---|
| 38 | if ( s_lastSchema!=schema ) |
|---|
| 39 | { |
|---|
| 40 | osgDB::ifstream schemaStream( schema.c_str(), std::ios::in ); |
|---|
| 41 | if ( !schemaStream.fail() ) readSchema( schemaStream ); |
|---|
| 42 | schemaStream.close(); |
|---|
| 43 | s_lastSchema = schema; |
|---|
| 44 | } |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | if ( schema.empty() ) |
|---|
| 48 | { |
|---|
| 49 | resetSchema(); |
|---|
| 50 | s_lastSchema.clear(); |
|---|
| 51 | } |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | InputStream::~InputStream() |
|---|
| 55 | { |
|---|
| 56 | if (_dataDecompress) |
|---|
| 57 | delete _dataDecompress; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | InputStream& InputStream::operator>>( osg::Vec2b& v ) |
|---|
| 61 | { |
|---|
| 62 | char x, y; *this >> x >> y; |
|---|
| 63 | v.set( x, y ); |
|---|
| 64 | return *this; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | InputStream& InputStream::operator>>( osg::Vec3b& v ) |
|---|
| 68 | { |
|---|
| 69 | char x, y, z; *this >> x >> y >> z; |
|---|
| 70 | v.set( x, y, z ); |
|---|
| 71 | return *this; |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | InputStream& InputStream::operator>>( osg::Vec4b& v ) |
|---|
| 75 | { |
|---|
| 76 | char x, y, z, w; *this >> x >> y >> z >> w; |
|---|
| 77 | v.set( x, y, z, w ); |
|---|
| 78 | return *this; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | InputStream& InputStream::operator>>( osg::Vec4ub& v ) |
|---|
| 82 | { |
|---|
| 83 | char r, g, b, a; *this >> r >> g >> b >> a; |
|---|
| 84 | v.set( r, g, b, a ); |
|---|
| 85 | return *this; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | InputStream& InputStream::operator>>( osg::Vec2s& v ) |
|---|
| 89 | { *this >> v.x() >> v.y(); return *this; } |
|---|
| 90 | |
|---|
| 91 | InputStream& InputStream::operator>>( osg::Vec3s& v ) |
|---|
| 92 | { *this >> v.x() >> v.y() >> v.z(); return *this; } |
|---|
| 93 | |
|---|
| 94 | InputStream& InputStream::operator>>( osg::Vec4s& v ) |
|---|
| 95 | { *this >> v.x() >> v.y() >> v.z() >> v.w(); return *this; } |
|---|
| 96 | |
|---|
| 97 | InputStream& InputStream::operator>>( osg::Vec2f& v ) |
|---|
| 98 | { *this >> v.x() >> v.y(); return *this; } |
|---|
| 99 | |
|---|
| 100 | InputStream& InputStream::operator>>( osg::Vec3f& v ) |
|---|
| 101 | { *this >> v.x() >> v.y() >> v.z(); return *this; } |
|---|
| 102 | |
|---|
| 103 | InputStream& InputStream::operator>>( osg::Vec4f& v ) |
|---|
| 104 | { *this >> v.x() >> v.y() >> v.z() >> v.w(); return *this; } |
|---|
| 105 | |
|---|
| 106 | InputStream& InputStream::operator>>( osg::Vec2d& v ) |
|---|
| 107 | { *this >> v.x() >> v.y(); return *this; } |
|---|
| 108 | |
|---|
| 109 | InputStream& InputStream::operator>>( osg::Vec3d& v ) |
|---|
| 110 | { *this >> v.x() >> v.y() >> v.z(); return *this; } |
|---|
| 111 | |
|---|
| 112 | InputStream& InputStream::operator>>( osg::Vec4d& v ) |
|---|
| 113 | { *this >> v.x() >> v.y() >> v.z() >> v.w(); return *this; } |
|---|
| 114 | |
|---|
| 115 | InputStream& InputStream::operator>>( osg::Quat& q ) |
|---|
| 116 | { *this >> q.x() >> q.y() >> q.z() >> q.w(); return *this; } |
|---|
| 117 | |
|---|
| 118 | InputStream& InputStream::operator>>( osg::Plane& p ) |
|---|
| 119 | { |
|---|
| 120 | double p0, p1, p2, p3; *this >> p0 >> p1 >> p2 >> p3; |
|---|
| 121 | p.set( p0, p1, p2, p3 ); return *this; |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | #if 0 |
|---|
| 125 | InputStream& InputStream::operator>>( osg::Matrixf& mat ) |
|---|
| 126 | { |
|---|
| 127 | ObjectProperty property(""); |
|---|
| 128 | *this >> property >> BEGIN_BRACKET; |
|---|
| 129 | |
|---|
| 130 | if (property._name == "Matrixf") |
|---|
| 131 | { |
|---|
| 132 | |
|---|
| 133 | for ( int r=0; r<4; ++r ) |
|---|
| 134 | { |
|---|
| 135 | *this >> mat(r, 0) >> mat(r, 1) >> mat(r, 2) >> mat(r, 3); |
|---|
| 136 | } |
|---|
| 137 | } |
|---|
| 138 | else if (property._name == "Matrixd") |
|---|
| 139 | { |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | double value; |
|---|
| 143 | for ( int r=0; r<4; ++r ) |
|---|
| 144 | { |
|---|
| 145 | for ( int c=0; c<4; ++c) |
|---|
| 146 | { |
|---|
| 147 | *this >> value; |
|---|
| 148 | mat(r,c) = static_cast<float>(value); |
|---|
| 149 | } |
|---|
| 150 | } |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | *this >> END_BRACKET; |
|---|
| 154 | return *this; |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | InputStream& InputStream::operator>>( osg::Matrixd& mat ) |
|---|
| 158 | { |
|---|
| 159 | ObjectProperty property(""); |
|---|
| 160 | *this >> property >> BEGIN_BRACKET; |
|---|
| 161 | |
|---|
| 162 | if (property._name == "Matrixf") |
|---|
| 163 | { |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | float value; |
|---|
| 167 | for ( int r=0; r<4; ++r ) |
|---|
| 168 | { |
|---|
| 169 | for ( int c=0; c<4; ++c) |
|---|
| 170 | { |
|---|
| 171 | *this >> value; |
|---|
| 172 | mat(r,c) = static_cast<float>(value); |
|---|
| 173 | } |
|---|
| 174 | } |
|---|
| 175 | } |
|---|
| 176 | else if (property._name == "Matrixd") |
|---|
| 177 | { |
|---|
| 178 | |
|---|
| 179 | for ( int r=0; r<4; ++r ) |
|---|
| 180 | { |
|---|
| 181 | *this >> mat(r, 0) >> mat(r, 1) >> mat(r, 2) >> mat(r, 3); |
|---|
| 182 | } |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | *this >> END_BRACKET; |
|---|
| 186 | return *this; |
|---|
| 187 | } |
|---|
| 188 | #else |
|---|
| 189 | InputStream& InputStream::operator>>( osg::Matrixf& mat ) |
|---|
| 190 | { |
|---|
| 191 | *this >> BEGIN_BRACKET; |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | |
|---|
| 195 | double value; |
|---|
| 196 | for ( int r=0; r<4; ++r ) |
|---|
| 197 | { |
|---|
| 198 | for ( int c=0; c<4; ++c) |
|---|
| 199 | { |
|---|
| 200 | *this >> value; |
|---|
| 201 | mat(r,c) = static_cast<float>(value); |
|---|
| 202 | } |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | *this >> END_BRACKET; |
|---|
| 206 | return *this; |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | InputStream& InputStream::operator>>( osg::Matrixd& mat ) |
|---|
| 210 | { |
|---|
| 211 | *this >> BEGIN_BRACKET; |
|---|
| 212 | |
|---|
| 213 | for ( int r=0; r<4; ++r ) |
|---|
| 214 | { |
|---|
| 215 | *this >> mat(r, 0) >> mat(r, 1) >> mat(r, 2) >> mat(r, 3); |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | *this >> END_BRACKET; |
|---|
| 219 | return *this; |
|---|
| 220 | } |
|---|
| 221 | #endif |
|---|
| 222 | |
|---|
| 223 | osg::Array* InputStream::readArray() |
|---|
| 224 | { |
|---|
| 225 | osg::ref_ptr<osg::Array> array = NULL; |
|---|
| 226 | |
|---|
| 227 | unsigned int id = 0; |
|---|
| 228 | *this >> PROPERTY("ArrayID") >> id; |
|---|
| 229 | |
|---|
| 230 | ArrayMap::iterator itr = _arrayMap.find( id ); |
|---|
| 231 | if ( itr!=_arrayMap.end() ) |
|---|
| 232 | { |
|---|
| 233 | return itr->second.get(); |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | DEF_MAPPEE(ArrayType, type); |
|---|
| 237 | *this >> type; |
|---|
| 238 | switch ( type.get() ) |
|---|
| 239 | { |
|---|
| 240 | case ID_BYTE_ARRAY: |
|---|
| 241 | { |
|---|
| 242 | osg::ByteArray* ba = new osg::ByteArray; |
|---|
| 243 | readArrayImplementation( ba, 1, CHAR_SIZE); |
|---|
| 244 | array = ba; |
|---|
| 245 | } |
|---|
| 246 | break; |
|---|
| 247 | case ID_UBYTE_ARRAY: |
|---|
| 248 | { |
|---|
| 249 | osg::UByteArray* uba = new osg::UByteArray; |
|---|
| 250 | readArrayImplementation( uba, 1, CHAR_SIZE ); |
|---|
| 251 | array = uba; |
|---|
| 252 | } |
|---|
| 253 | break; |
|---|
| 254 | case ID_SHORT_ARRAY: |
|---|
| 255 | { |
|---|
| 256 | osg::ShortArray* sa = new osg::ShortArray; |
|---|
| 257 | readArrayImplementation( sa, 1, SHORT_SIZE ); |
|---|
| 258 | array = sa; |
|---|
| 259 | } |
|---|
| 260 | break; |
|---|
| 261 | case ID_USHORT_ARRAY: |
|---|
| 262 | { |
|---|
| 263 | osg::UShortArray* usa = new osg::UShortArray; |
|---|
| 264 | readArrayImplementation( usa, 1, SHORT_SIZE ); |
|---|
| 265 | array = usa; |
|---|
| 266 | } |
|---|
| 267 | break; |
|---|
| 268 | case ID_INT_ARRAY: |
|---|
| 269 | { |
|---|
| 270 | osg::IntArray* ia = new osg::IntArray; |
|---|
| 271 | readArrayImplementation( ia, 1, INT_SIZE ); |
|---|
| 272 | array = ia; |
|---|
| 273 | } |
|---|
| 274 | break; |
|---|
| 275 | case ID_UINT_ARRAY: |
|---|
| 276 | { |
|---|
| 277 | osg::UIntArray* uia = new osg::UIntArray; |
|---|
| 278 | readArrayImplementation( uia, 1, INT_SIZE ); |
|---|
| 279 | array = uia; |
|---|
| 280 | } |
|---|
| 281 | break; |
|---|
| 282 | case ID_FLOAT_ARRAY: |
|---|
| 283 | { |
|---|
| 284 | osg::FloatArray* fa = new osg::FloatArray; |
|---|
| 285 | readArrayImplementation( fa, 1, FLOAT_SIZE ); |
|---|
| 286 | array = fa; |
|---|
| 287 | } |
|---|
| 288 | break; |
|---|
| 289 | case ID_DOUBLE_ARRAY: |
|---|
| 290 | { |
|---|
| 291 | osg::DoubleArray* da = new osg::DoubleArray; |
|---|
| 292 | readArrayImplementation( da, 1, DOUBLE_SIZE ); |
|---|
| 293 | array = da; |
|---|
| 294 | } |
|---|
| 295 | break; |
|---|
| 296 | case ID_VEC2B_ARRAY: |
|---|
| 297 | { |
|---|
| 298 | osg::Vec2bArray* va = new osg::Vec2bArray; |
|---|
| 299 | readArrayImplementation( va, 2, CHAR_SIZE ); |
|---|
| 300 | array = va; |
|---|
| 301 | } |
|---|
| 302 | break; |
|---|
| 303 | case ID_VEC3B_ARRAY: |
|---|
| 304 | { |
|---|
| 305 | osg::Vec3bArray* va = new osg::Vec3bArray; |
|---|
| 306 | readArrayImplementation( va, 3, CHAR_SIZE ); |
|---|
| 307 | array = va; |
|---|
| 308 | } |
|---|
| 309 | break; |
|---|
| 310 | case ID_VEC4B_ARRAY: |
|---|
| 311 | { |
|---|
| 312 | osg::Vec4bArray* va = new osg::Vec4bArray; |
|---|
| 313 | readArrayImplementation( va, 4, CHAR_SIZE ); |
|---|
| 314 | array = va; |
|---|
| 315 | } |
|---|
| 316 | break; |
|---|
| 317 | case ID_VEC4UB_ARRAY: |
|---|
| 318 | { |
|---|
| 319 | osg::Vec4ubArray* va = new osg::Vec4ubArray; |
|---|
| 320 | readArrayImplementation( va, 4, CHAR_SIZE ); |
|---|
| 321 | array = va; |
|---|
| 322 | } |
|---|
| 323 | break; |
|---|
| 324 | case ID_VEC2S_ARRAY: |
|---|
| 325 | { |
|---|
| 326 | osg::Vec2sArray* va = new osg::Vec2sArray; |
|---|
| 327 | readArrayImplementation( va, 2, SHORT_SIZE ); |
|---|
| 328 | array = va; |
|---|
| 329 | } |
|---|
| 330 | break; |
|---|
| 331 | case ID_VEC3S_ARRAY: |
|---|
| 332 | { |
|---|
| 333 | osg::Vec3sArray* va = new osg::Vec3sArray; |
|---|
| 334 | readArrayImplementation( va, 3, SHORT_SIZE ); |
|---|
| 335 | array = va; |
|---|
| 336 | } |
|---|
| 337 | break; |
|---|
| 338 | case ID_VEC4S_ARRAY: |
|---|
| 339 | { |
|---|
| 340 | osg::Vec4sArray* va = new osg::Vec4sArray; |
|---|
| 341 | readArrayImplementation( va, 4, SHORT_SIZE ); |
|---|
| 342 | array = va; |
|---|
| 343 | } |
|---|
| 344 | break; |
|---|
| 345 | case ID_VEC2_ARRAY: |
|---|
| 346 | { |
|---|
| 347 | osg::Vec2Array* va = new osg::Vec2Array; |
|---|
| 348 | readArrayImplementation( va, 2, FLOAT_SIZE ); |
|---|
| 349 | array = va; |
|---|
| 350 | } |
|---|
| 351 | break; |
|---|
| 352 | case ID_VEC3_ARRAY: |
|---|
| 353 | { |
|---|
| 354 | osg::Vec3Array* va = new osg::Vec3Array; |
|---|
| 355 | readArrayImplementation( va, 3, FLOAT_SIZE ); |
|---|
| 356 | array = va; |
|---|
| 357 | } |
|---|
| 358 | break; |
|---|
| 359 | case ID_VEC4_ARRAY: |
|---|
| 360 | { |
|---|
| 361 | osg::Vec4Array* va = new osg::Vec4Array; |
|---|
| 362 | readArrayImplementation( va, 4, FLOAT_SIZE ); |
|---|
| 363 | array = va; |
|---|
| 364 | } |
|---|
| 365 | break; |
|---|
| 366 | case ID_VEC2D_ARRAY: |
|---|
| 367 | { |
|---|
| 368 | osg::Vec2dArray* va = new osg::Vec2dArray; |
|---|
| 369 | readArrayImplementation( va, 2, DOUBLE_SIZE ); |
|---|
| 370 | array = va; |
|---|
| 371 | } |
|---|
| 372 | break; |
|---|
| 373 | case ID_VEC3D_ARRAY: |
|---|
| 374 | { |
|---|
| 375 | osg::Vec3dArray* va = new osg::Vec3dArray; |
|---|
| 376 | readArrayImplementation( va, 3, DOUBLE_SIZE ); |
|---|
| 377 | array = va; |
|---|
| 378 | } |
|---|
| 379 | break; |
|---|
| 380 | case ID_VEC4D_ARRAY: |
|---|
| 381 | { |
|---|
| 382 | osg::Vec4dArray* va = new osg::Vec4dArray; |
|---|
| 383 | readArrayImplementation( va, 4, DOUBLE_SIZE ); |
|---|
| 384 | array = va; |
|---|
| 385 | } |
|---|
| 386 | break; |
|---|
| 387 | default: |
|---|
| 388 | throwException( "InputStream::readArray(): Unsupported array type." ); |
|---|
| 389 | } |
|---|
| 390 | |
|---|
| 391 | if ( getException() ) return NULL; |
|---|
| 392 | _arrayMap[id] = array; |
|---|
| 393 | |
|---|
| 394 | return array.release(); |
|---|
| 395 | } |
|---|
| 396 | |
|---|
| 397 | osg::PrimitiveSet* InputStream::readPrimitiveSet() |
|---|
| 398 | { |
|---|
| 399 | osg::ref_ptr<osg::PrimitiveSet> primitive = NULL; |
|---|
| 400 | |
|---|
| 401 | DEF_MAPPEE(PrimitiveType, type); |
|---|
| 402 | DEF_MAPPEE(PrimitiveType, mode); |
|---|
| 403 | *this >> type >> mode; |
|---|
| 404 | |
|---|
| 405 | switch ( type.get() ) |
|---|
| 406 | { |
|---|
| 407 | case ID_DRAWARRAYS: |
|---|
| 408 | { |
|---|
| 409 | int first = 0, count = 0; |
|---|
| 410 | *this >> first >> count; |
|---|
| 411 | osg::DrawArrays* da = new osg::DrawArrays( mode.get(), first, count ); |
|---|
| 412 | primitive = da; |
|---|
| 413 | } |
|---|
| 414 | break; |
|---|
| 415 | case ID_DRAWARRAY_LENGTH: |
|---|
| 416 | { |
|---|
| 417 | int first = 0, value = 0; unsigned int size = 0; |
|---|
| 418 | *this >> first >> size >> BEGIN_BRACKET; |
|---|
| 419 | osg::DrawArrayLengths* dl = new osg::DrawArrayLengths( mode.get(), first ); |
|---|
| 420 | for ( unsigned int i=0; i<size; ++i ) |
|---|
| 421 | { |
|---|
| 422 | *this >> value; |
|---|
| 423 | dl->push_back( value ); |
|---|
| 424 | } |
|---|
| 425 | *this >> END_BRACKET; |
|---|
| 426 | primitive = dl; |
|---|
| 427 | } |
|---|
| 428 | break; |
|---|
| 429 | case ID_DRAWELEMENTS_UBYTE: |
|---|
| 430 | { |
|---|
| 431 | osg::DrawElementsUByte* de = new osg::DrawElementsUByte( mode.get() ); |
|---|
| 432 | unsigned int size = 0; unsigned char value = 0; |
|---|
| 433 | *this >> size >> BEGIN_BRACKET; |
|---|
| 434 | for ( unsigned int i=0; i<size; ++i ) |
|---|
| 435 | { |
|---|
| 436 | *this >> value; |
|---|
| 437 | de->push_back( value ); |
|---|
| 438 | } |
|---|
| 439 | *this >> END_BRACKET; |
|---|
| 440 | primitive = de; |
|---|
| 441 | } |
|---|
| 442 | break; |
|---|
| 443 | case ID_DRAWELEMENTS_USHORT: |
|---|
| 444 | { |
|---|
| 445 | osg::DrawElementsUShort* de = new osg::DrawElementsUShort( mode.get() ); |
|---|
| 446 | unsigned int size = 0; unsigned short value = 0; |
|---|
| 447 | *this >> size >> BEGIN_BRACKET; |
|---|
| 448 | for ( unsigned int i=0; i<size; ++i ) |
|---|
| 449 | { |
|---|
| 450 | *this >> value; |
|---|
| 451 | de->push_back( value ); |
|---|
| 452 | } |
|---|
| 453 | *this >> END_BRACKET; |
|---|
| 454 | primitive = de; |
|---|
| 455 | } |
|---|
| 456 | break; |
|---|
| 457 | case ID_DRAWELEMENTS_UINT: |
|---|
| 458 | { |
|---|
| 459 | osg::DrawElementsUInt* de = new osg::DrawElementsUInt( mode.get() ); |
|---|
| 460 | unsigned int size = 0, value = 0; |
|---|
| 461 | *this >> size >> BEGIN_BRACKET; |
|---|
| 462 | for ( unsigned int i=0; i<size; ++i ) |
|---|
| 463 | { |
|---|
| 464 | *this >> value; |
|---|
| 465 | de->push_back( value ); |
|---|
| 466 | } |
|---|
| 467 | *this >> END_BRACKET; |
|---|
| 468 | primitive = de; |
|---|
| 469 | } |
|---|
| 470 | break; |
|---|
| 471 | default: |
|---|
| 472 | throwException( "InputStream::readPrimitiveSet(): Unsupported array type." ); |
|---|
| 473 | } |
|---|
| 474 | |
|---|
| 475 | if ( getException() ) return NULL; |
|---|
| 476 | return primitive.release(); |
|---|
| 477 | } |
|---|
| 478 | |
|---|
| 479 | osg::Image* InputStream::readImage(bool readFromExternal) |
|---|
| 480 | { |
|---|
| 481 | |
|---|
| 482 | std::string className="osg::Image"; |
|---|
| 483 | unsigned int id = 0; |
|---|
| 484 | |
|---|
| 485 | *this >> PROPERTY("UniqueID") >> id; |
|---|
| 486 | if ( getException() ) return NULL; |
|---|
| 487 | |
|---|
| 488 | IdentifierMap::iterator itr = _identifierMap.find( id ); |
|---|
| 489 | if ( itr!=_identifierMap.end() ) |
|---|
| 490 | { |
|---|
| 491 | return static_cast<osg::Image*>( itr->second.get() ); |
|---|
| 492 | } |
|---|
| 493 | |
|---|
| 494 | std::string name; |
|---|
| 495 | int writeHint, decision = IMAGE_EXTERNAL; |
|---|
| 496 | *this >> PROPERTY("FileName"); readWrappedString(name); |
|---|
| 497 | *this >> PROPERTY("WriteHint") >> writeHint >> decision; |
|---|
| 498 | if ( getException() ) return NULL; |
|---|
| 499 | |
|---|
| 500 | osg::ref_ptr<osg::Image> image = NULL; |
|---|
| 501 | |
|---|
| 502 | switch ( decision ) |
|---|
| 503 | { |
|---|
| 504 | case IMAGE_INLINE_DATA: |
|---|
| 505 | if ( isBinary() ) |
|---|
| 506 | { |
|---|
| 507 | image = new osg::Image; |
|---|
| 508 | |
|---|
| 509 | |
|---|
| 510 | int origin, s, t, r, internalFormat; |
|---|
| 511 | *this >> origin >> s >> t >> r >> internalFormat; |
|---|
| 512 | |
|---|
| 513 | |
|---|
| 514 | int pixelFormat, dataType, packing, mode; |
|---|
| 515 | *this >> pixelFormat >> dataType >> packing >> mode; |
|---|
| 516 | |
|---|
| 517 | |
|---|
| 518 | unsigned int size = 0; *this >> size; |
|---|
| 519 | if ( size ) |
|---|
| 520 | { |
|---|
| 521 | char* data = new char[size]; |
|---|
| 522 | if ( !data ) |
|---|
| 523 | throwException( "InputStream::readImage() Out of memory." ); |
|---|
| 524 | if ( getException() ) return NULL; |
|---|
| 525 | |
|---|
| 526 | readCharArray( data, size ); |
|---|
| 527 | image->setOrigin( (osg::Image::Origin)origin ); |
|---|
| 528 | image->setImage( s, t, r, internalFormat, pixelFormat, dataType, |
|---|
| 529 | (unsigned char*)data, (osg::Image::AllocationMode)mode, packing ); |
|---|
| 530 | } |
|---|
| 531 | |
|---|
| 532 | |
|---|
| 533 | unsigned int levelSize = readSize(); |
|---|
| 534 | osg::Image::MipmapDataType levels(levelSize); |
|---|
| 535 | for ( unsigned int i=0; i<levelSize; ++i ) |
|---|
| 536 | { |
|---|
| 537 | *this >> levels[i]; |
|---|
| 538 | } |
|---|
| 539 | if ( levelSize>0 ) |
|---|
| 540 | image->setMipmapLevels( levels ); |
|---|
| 541 | readFromExternal = false; |
|---|
| 542 | } |
|---|
| 543 | break; |
|---|
| 544 | case IMAGE_INLINE_FILE: |
|---|
| 545 | if ( isBinary() ) |
|---|
| 546 | { |
|---|
| 547 | unsigned int size = readSize(); |
|---|
| 548 | if ( size>0 ) |
|---|
| 549 | { |
|---|
| 550 | char* data = new char[size]; |
|---|
| 551 | if ( !data ) |
|---|
| 552 | { |
|---|
| 553 | throwException( "InputStream::readImage(): Out of memory." ); |
|---|
| 554 | if ( getException() ) return NULL; |
|---|
| 555 | } |
|---|
| 556 | readCharArray( data, size ); |
|---|
| 557 | |
|---|
| 558 | std::string ext = osgDB::getFileExtension( name ); |
|---|
| 559 | osgDB::ReaderWriter* reader = |
|---|
| 560 | osgDB::Registry::instance()->getReaderWriterForExtension( ext ); |
|---|
| 561 | if ( reader ) |
|---|
| 562 | { |
|---|
| 563 | std::stringstream inputStream; |
|---|
| 564 | inputStream.write( data, size ); |
|---|
| 565 | |
|---|
| 566 | osgDB::ReaderWriter::ReadResult rr = reader->readImage( inputStream ); |
|---|
| 567 | if ( rr.validImage() ) |
|---|
| 568 | image = rr.takeImage(); |
|---|
| 569 | else |
|---|
| 570 | { |
|---|
| 571 | OSG_WARN << "InputStream::readImage(): " |
|---|
| 572 | << rr.message() << std::endl; |
|---|
| 573 | } |
|---|
| 574 | } |
|---|
| 575 | else |
|---|
| 576 | { |
|---|
| 577 | OSG_WARN << "InputStream::readImage(): Unable to find a plugin for " |
|---|
| 578 | << ext << std::endl; |
|---|
| 579 | } |
|---|
| 580 | delete[] data; |
|---|
| 581 | } |
|---|
| 582 | readFromExternal = false; |
|---|
| 583 | } |
|---|
| 584 | break; |
|---|
| 585 | case IMAGE_EXTERNAL: case IMAGE_WRITE_OUT: |
|---|
| 586 | break; |
|---|
| 587 | default: |
|---|
| 588 | break; |
|---|
| 589 | } |
|---|
| 590 | |
|---|
| 591 | if ( readFromExternal ) |
|---|
| 592 | { |
|---|
| 593 | image = osgDB::readImageFile( name, getOptions() ); |
|---|
| 594 | if ( !image && _forceReadingImage ) image = new osg::Image; |
|---|
| 595 | } |
|---|
| 596 | if ( image.valid() ) |
|---|
| 597 | { |
|---|
| 598 | image->setFileName( name ); |
|---|
| 599 | image->setWriteHint( (osg::Image::WriteHint)writeHint ); |
|---|
| 600 | } |
|---|
| 601 | |
|---|
| 602 | image = static_cast<osg::Image*>( readObjectFields(className, id, image.get()) ); |
|---|
| 603 | |
|---|
| 604 | return image.release(); |
|---|
| 605 | } |
|---|
| 606 | |
|---|
| 607 | osg::Object* InputStream::readObject( osg::Object* existingObj ) |
|---|
| 608 | { |
|---|
| 609 | std::string className; |
|---|
| 610 | unsigned int id = 0; |
|---|
| 611 | *this >> className >> BEGIN_BRACKET >> PROPERTY("UniqueID") >> id; |
|---|
| 612 | if ( getException() ) return NULL; |
|---|
| 613 | |
|---|
| 614 | IdentifierMap::iterator itr = _identifierMap.find( id ); |
|---|
| 615 | if ( itr!=_identifierMap.end() ) |
|---|
| 616 | { |
|---|
| 617 | advanceToCurrentEndBracket(); |
|---|
| 618 | return itr->second.get(); |
|---|
| 619 | } |
|---|
| 620 | |
|---|
| 621 | osg::ref_ptr<osg::Object> obj = readObjectFields( className, id, existingObj ); |
|---|
| 622 | |
|---|
| 623 | advanceToCurrentEndBracket(); |
|---|
| 624 | |
|---|
| 625 | return obj.release(); |
|---|
| 626 | } |
|---|
| 627 | |
|---|
| 628 | osg::Object* InputStream::readObjectFields( const std::string& className, unsigned int id, osg::Object* existingObj ) |
|---|
| 629 | { |
|---|
| 630 | ObjectWrapper* wrapper = Registry::instance()->getObjectWrapperManager()->findWrapper( className ); |
|---|
| 631 | if ( !wrapper ) |
|---|
| 632 | { |
|---|
| 633 | OSG_WARN << "InputStream::readObject(): Unsupported wrapper class " |
|---|
| 634 | << className << std::endl; |
|---|
| 635 | return NULL; |
|---|
| 636 | } |
|---|
| 637 | _fields.push_back( className ); |
|---|
| 638 | |
|---|
| 639 | osg::ref_ptr<osg::Object> obj = existingObj ? existingObj : wrapper->getProto()->cloneType(); |
|---|
| 640 | _identifierMap[id] = obj; |
|---|
| 641 | if ( obj.valid() ) |
|---|
| 642 | { |
|---|
| 643 | const StringList& associates = wrapper->getAssociates(); |
|---|
| 644 | for ( StringList::const_iterator itr=associates.begin(); itr!=associates.end(); ++itr ) |
|---|
| 645 | { |
|---|
| 646 | ObjectWrapper* assocWrapper = Registry::instance()->getObjectWrapperManager()->findWrapper(*itr); |
|---|
| 647 | if ( !assocWrapper ) |
|---|
| 648 | { |
|---|
| 649 | OSG_WARN << "InputStream::readObject(): Unsupported associated class " |
|---|
| 650 | << *itr << std::endl; |
|---|
| 651 | continue; |
|---|
| 652 | } |
|---|
| 653 | _fields.push_back( assocWrapper->getName() ); |
|---|
| 654 | |
|---|
| 655 | assocWrapper->read( *this, *obj ); |
|---|
| 656 | if ( getException() ) return NULL; |
|---|
| 657 | |
|---|
| 658 | _fields.pop_back(); |
|---|
| 659 | } |
|---|
| 660 | } |
|---|
| 661 | _fields.pop_back(); |
|---|
| 662 | return obj.release(); |
|---|
| 663 | } |
|---|
| 664 | |
|---|
| 665 | void InputStream::readSchema( std::istream& fin ) |
|---|
| 666 | { |
|---|
| 667 | |
|---|
| 668 | std::string line; |
|---|
| 669 | while ( std::getline(fin, line) ) |
|---|
| 670 | { |
|---|
| 671 | if ( line[0]=='#' ) continue; |
|---|
| 672 | |
|---|
| 673 | StringList keyAndValue; |
|---|
| 674 | split( line, keyAndValue, '=' ); |
|---|
| 675 | if ( keyAndValue.size()<2 ) continue; |
|---|
| 676 | |
|---|
| 677 | setWrapperSchema( osgDB::trimEnclosingSpaces(keyAndValue[0]), |
|---|
| 678 | osgDB::trimEnclosingSpaces(keyAndValue[1]) ); |
|---|
| 679 | } |
|---|
| 680 | } |
|---|
| 681 | |
|---|
| 682 | InputStream::ReadType InputStream::start( InputIterator* inIterator ) |
|---|
| 683 | { |
|---|
| 684 | _fields.clear(); |
|---|
| 685 | _fields.push_back( "Start" ); |
|---|
| 686 | |
|---|
| 687 | ReadType type = READ_UNKNOWN; |
|---|
| 688 | _in = inIterator; |
|---|
| 689 | if ( !_in ) |
|---|
| 690 | throwException( "InputStream: Null stream specified." ); |
|---|
| 691 | if ( getException() ) return type; |
|---|
| 692 | |
|---|
| 693 | _in->setInputStream(this); |
|---|
| 694 | |
|---|
| 695 | |
|---|
| 696 | unsigned int version = 0; |
|---|
| 697 | if ( isBinary() ) |
|---|
| 698 | { |
|---|
| 699 | unsigned int typeValue; |
|---|
| 700 | *this >> typeValue >> version; |
|---|
| 701 | type = static_cast<ReadType>(typeValue); |
|---|
| 702 | |
|---|
| 703 | unsigned int attributes; *this >> attributes; |
|---|
| 704 | if ( attributes&0x2 ) _useSchemaData = true; |
|---|
| 705 | } |
|---|
| 706 | if ( !isBinary() ) |
|---|
| 707 | { |
|---|
| 708 | std::string typeString; *this >> typeString; |
|---|
| 709 | if ( typeString=="Scene" ) type = READ_SCENE; |
|---|
| 710 | else if ( typeString=="Image" ) type = READ_IMAGE; |
|---|
| 711 | else if ( typeString=="Object" ) type = READ_OBJECT; |
|---|
| 712 | |
|---|
| 713 | std::string osgName, osgVersion; |
|---|
| 714 | *this >> PROPERTY("#Version") >> version; |
|---|
| 715 | *this >> PROPERTY("#Generator") >> osgName >> osgVersion; |
|---|
| 716 | } |
|---|
| 717 | |
|---|
| 718 | |
|---|
| 719 | _fileVersion = version; |
|---|
| 720 | _fields.pop_back(); |
|---|
| 721 | return type; |
|---|
| 722 | } |
|---|
| 723 | |
|---|
| 724 | void InputStream::decompress() |
|---|
| 725 | { |
|---|
| 726 | if ( !isBinary() ) return; |
|---|
| 727 | _fields.clear(); |
|---|
| 728 | |
|---|
| 729 | std::string compressorName; *this >> compressorName; |
|---|
| 730 | if ( compressorName!="0" ) |
|---|
| 731 | { |
|---|
| 732 | std::string data; |
|---|
| 733 | _fields.push_back( "Decompression" ); |
|---|
| 734 | |
|---|
| 735 | BaseCompressor* compressor = Registry::instance()->getObjectWrapperManager()->findCompressor(compressorName); |
|---|
| 736 | if ( !compressor ) |
|---|
| 737 | { |
|---|
| 738 | OSG_WARN << "InputStream::decompress(): No such compressor " |
|---|
| 739 | << compressorName << std::endl; |
|---|
| 740 | } |
|---|
| 741 | |
|---|
| 742 | if ( !compressor->decompress(*(_in->getStream()), data) ) |
|---|
| 743 | throwException( "InputStream: Failed to decompress stream." ); |
|---|
| 744 | if ( getException() ) return; |
|---|
| 745 | |
|---|
| 746 | _dataDecompress = new std::stringstream(data); |
|---|
| 747 | _in->setStream( _dataDecompress ); |
|---|
| 748 | _fields.pop_back(); |
|---|
| 749 | } |
|---|
| 750 | |
|---|
| 751 | if ( _useSchemaData ) |
|---|
| 752 | { |
|---|
| 753 | _fields.push_back( "SchemaData" ); |
|---|
| 754 | std::string schemaSource; *this >> schemaSource; |
|---|
| 755 | std::istringstream iss( schemaSource ); |
|---|
| 756 | readSchema( iss ); |
|---|
| 757 | _fields.pop_back(); |
|---|
| 758 | } |
|---|
| 759 | } |
|---|
| 760 | |
|---|
| 761 | |
|---|
| 762 | |
|---|
| 763 | void InputStream::setWrapperSchema( const std::string& name, const std::string& properties ) |
|---|
| 764 | { |
|---|
| 765 | ObjectWrapper* wrapper = Registry::instance()->getObjectWrapperManager()->findWrapper(name); |
|---|
| 766 | if ( !wrapper ) |
|---|
| 767 | { |
|---|
| 768 | OSG_WARN << "InputStream::setSchema(): Unsupported wrapper class " |
|---|
| 769 | << name << std::endl; |
|---|
| 770 | return; |
|---|
| 771 | } |
|---|
| 772 | |
|---|
| 773 | StringList schema, methods, keyAndValue; |
|---|
| 774 | std::vector<int> types; |
|---|
| 775 | split( properties, schema ); |
|---|
| 776 | for ( StringList::iterator itr=schema.begin(); itr!=schema.end(); ++itr ) |
|---|
| 777 | { |
|---|
| 778 | split( *itr, keyAndValue, ':' ); |
|---|
| 779 | if ( keyAndValue.size()>1 ) |
|---|
| 780 | { |
|---|
| 781 | methods.push_back( keyAndValue.front() ); |
|---|
| 782 | types.push_back( atoi(keyAndValue.back().c_str()) ); |
|---|
| 783 | } |
|---|
| 784 | else |
|---|
| 785 | { |
|---|
| 786 | methods.push_back( *itr ); |
|---|
| 787 | types.push_back( 0 ); |
|---|
| 788 | } |
|---|
| 789 | keyAndValue.clear(); |
|---|
| 790 | } |
|---|
| 791 | wrapper->readSchema( methods, types ); |
|---|
| 792 | } |
|---|
| 793 | |
|---|
| 794 | void InputStream::resetSchema() |
|---|
| 795 | { |
|---|
| 796 | const ObjectWrapperManager::WrapperMap& wrappers = Registry::instance()->getObjectWrapperManager()->getWrapperMap(); |
|---|
| 797 | for ( ObjectWrapperManager::WrapperMap::const_iterator itr=wrappers.begin(); |
|---|
| 798 | itr!=wrappers.end(); ++itr ) |
|---|
| 799 | { |
|---|
| 800 | ObjectWrapper* wrapper = itr->second.get(); |
|---|
| 801 | wrapper->resetSchema(); |
|---|
| 802 | } |
|---|
| 803 | } |
|---|
| 804 | |
|---|
| 805 | template<typename T> |
|---|
| 806 | void InputStream::readArrayImplementation( T* a, unsigned int numComponentsPerElements, unsigned int componentSizeInBytes ) |
|---|
| 807 | { |
|---|
| 808 | int size = 0; |
|---|
| 809 | *this >> size >> BEGIN_BRACKET; |
|---|
| 810 | if ( size ) |
|---|
| 811 | { |
|---|
| 812 | a->resize( size ); |
|---|
| 813 | if ( isBinary() ) |
|---|
| 814 | { |
|---|
| 815 | readComponentArray( (char*)&((*a)[0]), size, numComponentsPerElements, componentSizeInBytes ); |
|---|
| 816 | checkStream(); |
|---|
| 817 | } |
|---|
| 818 | else |
|---|
| 819 | { |
|---|
| 820 | for ( int i=0; i<size; ++i ) |
|---|
| 821 | *this >> (*a)[i]; |
|---|
| 822 | } |
|---|
| 823 | } |
|---|
| 824 | *this >> END_BRACKET; |
|---|
| 825 | } |
|---|