Hi Colin and Rui,

I've made some more changes in attempt to keep InputStream and
InputStreamOperator's handling of the endian byte swap consistent, the
changes are detailed below.  I have also merged Colin's suggested
changes the the write/readLong(..) and write/readULong(..) methods
These changes are now checked into svn/trunk.

Could you review these changes and test them out and let me know if
they are sufficient or whether there is still some further work to do.

Thanks,
Robert.


OpenSceneGraph$ svn diff
Index: include/osgDB/InputStream
===================================================================
--- include/osgDB/InputStream   (revision 12998)
+++ include/osgDB/InputStream   (working copy)
@@ -140,6 +140,9 @@
     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; }

Index: include/osgDB/StreamOperator
===================================================================
--- include/osgDB/StreamOperator        (revision 12975)
+++ include/osgDB/StreamOperator        (working copy)
@@ -69,7 +69,7 @@
 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 @@
     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 @@
 protected:
     std::istream*       _in;
     osgDB::InputStream* _inputStream;
-    mutable bool _failed;
+    int                 _byteSwap;
+    mutable bool        _failed;
 };

 }
Index: src/osgPlugins/osg/BinaryStreamOperator.h
===================================================================
--- src/osgPlugins/osg/BinaryStreamOperator.h   (revision 13003)
+++ src/osgPlugins/osg/BinaryStreamOperator.h   (working copy)
@@ -3,6 +3,14 @@

 #include <osgDB/StreamOperator>

+#if defined(_MSC_VER)
+typedef unsigned __int32 uint32_t;
+typedef __int32 int32_t;
+#else
+#include <stdint.h>
+#endif
+
+
 class BinaryOutputIterator : public osgDB::OutputIterator
 {
 public:
@@ -33,10 +41,18 @@
     { _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 @@
 class BinaryInputIterator : public osgDB::InputIterator
 {
 public:
-    BinaryInputIterator( std::istream* istream, bool byteSwap ) :
_byteSwap(byteSwap) { _in = istream; }
+    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 @@

     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 @@

     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 @@
     { readString( str ); }

 protected:
-    bool _byteSwap;
 };

 #endif
Index: src/osgPlugins/osg/ReaderWriterOSG2.cpp
===================================================================
--- src/osgPlugins/osg/ReaderWriterOSG2.cpp     (revision 13003)
+++ src/osgPlugins/osg/ReaderWriterOSG2.cpp     (working copy)
@@ -46,12 +46,12 @@
         if ( headerLow==OSG_HEADER_LOW && headerHigh==OSG_HEADER_HIGH )
         {
             OSG_INFO<<"Reading OpenSceneGraph binary file with the
same endian as this computer."<<std::endl;
-            return new BinaryInputIterator(&fin, false); // endian
the same so no byte swap required
+            return new BinaryInputIterator(&fin, 0); // endian the
same so no byte swap required
         }
         else if ( headerLow==OSG_REVERSE(OSG_HEADER_LOW) &&
headerHigh==OSG_REVERSE(OSG_HEADER_HIGH) )
         {
             OSG_INFO<<"Reading OpenSceneGraph binary file with the
different endian to this computer, doing byte swap."<<std::endl;
-            return new BinaryInputIterator(&fin, true); // endian
different so byte swap required
+            return new BinaryInputIterator(&fin, 1); // endian
different so byte swap required
         }

         fin.seekg( 0, std::ios::beg );
Index: src/osgDB/InputStream.cpp
===================================================================
--- src/osgDB/InputStream.cpp   (revision 12998)
+++ src/osgDB/InputStream.cpp   (working copy)
@@ -683,6 +683,9 @@
 {
     _fields.clear();
     _fields.push_back( "Start" );
+
+    OSG_INFO<<"InputStream::start( InputIterator* inIterator )
calling setByteSwap("<<inIterator->getByteSwap()<<")"<<std::endl;
+    setByteSwap(inIterator->getByteSwap());

     ReadType type = READ_UNKNOWN;
     _in = inIterator;
_______________________________________________
osg-submissions mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-submissions-openscenegraph.org

Reply via email to