diff --git a/include/osgDB/InputStream b/include/osgDB/InputStream index cf0999815..3ea247004 100644 --- a/include/osgDB/InputStream +++ b/include/osgDB/InputStream @@ -140,6 +140,9 @@ public: osg::Object* readObject( osg::Object* existingObj=0 ); osg::Object* readObjectFields( const std::string& className, unsigned int id, osg::Object* existingObj=0); + void setByteSwap(int byteSwap) { _byteSwap = byteSwap; } + int getByteSwap() const { return _byteSwap; } + /// set an input iterator, used directly when not using InputStream with a traditional file releated stream. void setInputIterator( InputIterator* ii ) { _in = ii; } diff --git a/include/osgDB/StreamOperator b/include/osgDB/StreamOperator index 61c7f4daa..fdcb979a0 100644 --- a/include/osgDB/StreamOperator +++ b/include/osgDB/StreamOperator @@ -69,7 +69,7 @@ protected: class OSGDB_EXPORT InputIterator : public osg::Referenced { public: - InputIterator() : _in(0), _inputStream(0), _failed(false) {} + InputIterator() : _in(0), _inputStream(0), _byteSwap(0), _failed(false) {} virtual ~InputIterator() {} void setStream( std::istream* istream ) { _in = istream; } @@ -79,7 +79,10 @@ public: void setInputStream( InputStream* inputStream) { _inputStream = inputStream; } InputStream* getInputStream() { return _inputStream; } const InputStream* getInputStream() const { return _inputStream; } - + + void setByteSwap(int byteSwap) { _byteSwap = byteSwap; } + int getByteSwap() const { return _byteSwap; } + void checkStream() const { if (_in->rdstate()&_in->failbit) _failed = true; } bool isFailed() const { return _failed; } @@ -115,7 +118,8 @@ public: protected: std::istream* _in; osgDB::InputStream* _inputStream; - mutable bool _failed; + int _byteSwap; + mutable bool _failed; }; } diff --git a/src/osgDB/InputStream.cpp b/src/osgDB/InputStream.cpp index 47ed5ba64..5bc63e4e6 100644 --- a/src/osgDB/InputStream.cpp +++ b/src/osgDB/InputStream.cpp @@ -683,6 +683,9 @@ InputStream::ReadType InputStream::start( InputIterator* inIterator ) { _fields.clear(); _fields.push_back( "Start" ); + + OSG_INFO<<"InputStream::start( InputIterator* inIterator ) calling setByteSwap("<getByteSwap()<<")"<getByteSwap()); ReadType type = READ_UNKNOWN; _in = inIterator; diff --git a/src/osgPlugins/osg/BinaryStreamOperator.h b/src/osgPlugins/osg/BinaryStreamOperator.h index 7cfa1ca09..ae0881de2 100644 --- a/src/osgPlugins/osg/BinaryStreamOperator.h +++ b/src/osgPlugins/osg/BinaryStreamOperator.h @@ -3,6 +3,14 @@ #include +#if defined(_MSC_VER) +typedef unsigned __int32 uint32_t; +typedef __int32 int32_t; +#else +#include +#endif + + class BinaryOutputIterator : public osgDB::OutputIterator { public: @@ -33,10 +41,18 @@ public: { _out->write( (char*)&i, osgDB::INT_SIZE ); } virtual void writeLong( long l ) - { _out->write( (char*)&l, osgDB::LONG_SIZE ); } - + { + // On 64-bit systems a long may not be the same size as the file value + int32_t value=(int32_t)l; + _out->write( (char*)&value, osgDB::LONG_SIZE ); + } + virtual void writeULong( unsigned long l ) - { _out->write( (char*)&l, osgDB::LONG_SIZE ); } + { + // On 64-bit systems a long may not be the same size as the file value + uint32_t value=(int32_t)l; + _out->write( (char*)&value, osgDB::LONG_SIZE ); + } virtual void writeFloat( float f ) { _out->write( (char*)&f, osgDB::FLOAT_SIZE ); } @@ -73,9 +89,14 @@ public: class BinaryInputIterator : public osgDB::InputIterator { public: - BinaryInputIterator( std::istream* istream, bool byteSwap ) : _byteSwap(byteSwap) { _in = istream; } - virtual ~BinaryInputIterator() {} + BinaryInputIterator( std::istream* istream, int byteSwap ) + { + _in = istream; + setByteSwap(byteSwap); + } + virtual ~BinaryInputIterator() {} + virtual bool isBinary() const { return true; } virtual void readBool( bool& b ) @@ -120,14 +141,19 @@ public: virtual void readLong( long& l ) { - _in->read( (char*)&l, osgDB::LONG_SIZE ); - if ( _byteSwap ) osg::swapBytes( (char*)&l, osgDB::LONG_SIZE ); + // On 64-bit systems a long may not be the same size as the file value + int32_t value; + _in->read( (char*)&value, osgDB::LONG_SIZE ); + if ( _byteSwap ) osg::swapBytes( (char*)&value, osgDB::LONG_SIZE ); + l = (long)value; } virtual void readULong( unsigned long& l ) { - _in->read( (char*)&l, osgDB::LONG_SIZE ); - if ( _byteSwap ) osg::swapBytes( (char*)&l, osgDB::LONG_SIZE ); + uint32_t value; + _in->read( (char*)&value, osgDB::LONG_SIZE ); + if ( _byteSwap ) osg::swapBytes( (char*)&value, osgDB::LONG_SIZE ); + l = (unsigned long)value; } virtual void readFloat( float& f ) @@ -144,7 +170,8 @@ public: virtual void readString( std::string& s ) { - int size = 0; readInt( size ); + int size = 0; + readInt( size ); if ( size>0 ) { s.resize( size ); @@ -188,7 +215,6 @@ public: { readString( str ); } protected: - bool _byteSwap; }; #endif diff --git a/src/osgPlugins/osg/ReaderWriterOSG2.cpp b/src/osgPlugins/osg/ReaderWriterOSG2.cpp index 990e31bba..d4865cedf 100644 --- a/src/osgPlugins/osg/ReaderWriterOSG2.cpp +++ b/src/osgPlugins/osg/ReaderWriterOSG2.cpp @@ -46,12 +46,12 @@ InputIterator* readInputIterator( std::istream& fin, const Options* options ) if ( headerLow==OSG_HEADER_LOW && headerHigh==OSG_HEADER_HIGH ) { OSG_INFO<<"Reading OpenSceneGraph binary file with the same endian as this computer."<