Hi Colin and Rui,

I have worked to address the problem with byte swapping of osg::Array
incorrectly by putting the byte swap into a
OutputIterator::readComponentArray( char* s, unsigned int numElements,
unsigned int numComponentsPerElements, unsigned int
componentSizeInBytes) method.  This has made it possible to pull all
the byte swapping code from InputStream which makes things a little
cleaner.  I have also ammended InputStream to use the new method, see
changes below.  I haven't been able to test the changes fully yet as I
don't have any .osgb from a system with a different endian, but what
testing I have done on forcing a swap suggest at least some form or
byte swapping in now happening.

Colin could you review and test my changes.  Rui I'd welcome you have
a look over the code changes as well.  My changes are now checked into
svn/trunk.

Cheers,
Robert.

OpenSceneGraph$ svn diff
Index: include/osgDB/InputStream
===================================================================
--- include/osgDB/InputStream   (revision 13005)
+++ include/osgDB/InputStream   (working copy)
@@ -129,6 +129,7 @@
     void advanceToCurrentEndBracket() { _in->advanceToCurrentEndBracket(); }
     void readWrappedString( std::string& str ) {
_in->readWrappedString(str); checkStream(); }
     void readCharArray( char* s, unsigned int size ) {
_in->readCharArray(s, size); }
+    void readComponentArray( char* s, unsigned int numElements,
unsigned int numComponentsPerElements, unsigned int
componentSizeInBytes) { _in->readComponentArray( s, numElements,
numComponentsPerElements, componentSizeInBytes); }

     // readSize() use unsigned int for all sizes.
     unsigned int readSize() { unsigned int size; *this>>size; return size; }
@@ -140,9 +141,6 @@
     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; }

@@ -164,13 +162,12 @@
     void setWrapperSchema( const std::string& name, const
std::string& properties );

     template<typename T>
-    void readArrayImplementation( T* a, int read_size, bool
useByteSwap=false );
+    void readArrayImplementation( T* a, unsigned int
numComponentsPerElements, unsigned int componentSizeInBytes );

     ArrayMap _arrayMap;
     IdentifierMap _identifierMap;

     int _fileVersion;
-    int _byteSwap;
     bool _useSchemaData;
     bool _forceReadingImage;
     std::vector<std::string> _fields;
Index: include/osgDB/StreamOperator
===================================================================
--- include/osgDB/StreamOperator        (revision 13005)
+++ include/osgDB/StreamOperator        (working copy)
@@ -22,7 +22,7 @@
     void setStream( std::ostream* ostream ) { _out = ostream; }
     std::ostream* getStream() { return _out; }
     const std::ostream* getStream() const { return _out; }
-
+
     virtual bool isBinary() const = 0;

     virtual void writeBool( bool b ) = 0;
@@ -115,6 +115,8 @@

     void throwException( const std::string& msg );

+    void readComponentArray( char* s, unsigned int numElements,
unsigned int numComponentsPerElements, unsigned int
componentSizeInBytes);
+
 protected:
     std::istream*       _in;
     osgDB::InputStream* _inputStream;
Index: src/osgPlugins/osg/BinaryStreamOperator.h
===================================================================
--- src/osgPlugins/osg/BinaryStreamOperator.h   (revision 13005)
+++ src/osgPlugins/osg/BinaryStreamOperator.h   (working copy)
@@ -209,8 +209,8 @@
     virtual void readMark( osgDB::ObjectMark& mark ) {}

     virtual void readCharArray( char* s, unsigned int size )
-    { if ( size>0 ) _in->read( s, size ); }
-
+    { if ( size>0 ) _in->read( s, size ); }
+
     virtual void readWrappedString( std::string& str )
     { readString( str ); }

Index: src/osgDB/InputStream.cpp
===================================================================
--- src/osgDB/InputStream.cpp   (revision 13005)
+++ src/osgDB/InputStream.cpp   (working copy)
@@ -24,7 +24,7 @@
 static std::string s_lastSchema;

 InputStream::InputStream( const osgDB::Options* options )
-    :   _fileVersion(0), _byteSwap(0), _useSchemaData(false),
_forceReadingImage(false), _dataDecompress(0)
+    :   _fileVersion(0), _useSchemaData(false),
_forceReadingImage(false), _dataDecompress(0)
 {
     if ( !options ) return;
     _options = options;
@@ -240,147 +240,147 @@
     case ID_BYTE_ARRAY:
         {
             osg::ByteArray* ba = new osg::ByteArray;
-            readArrayImplementation( ba, CHAR_SIZE, true );
+            readArrayImplementation( ba, 1, CHAR_SIZE);
             array = ba;
         }
         break;
     case ID_UBYTE_ARRAY:
         {
             osg::UByteArray* uba = new osg::UByteArray;
-            readArrayImplementation( uba, CHAR_SIZE, true );
+            readArrayImplementation( uba, 1, CHAR_SIZE );
             array = uba;
         }
         break;
     case ID_SHORT_ARRAY:
         {
             osg::ShortArray* sa = new osg::ShortArray;
-            readArrayImplementation( sa, SHORT_SIZE, true );
+            readArrayImplementation( sa, 1, SHORT_SIZE );
             array = sa;
         }
         break;
     case ID_USHORT_ARRAY:
         {
             osg::UShortArray* usa = new osg::UShortArray;
-            readArrayImplementation( usa, SHORT_SIZE, true );
+            readArrayImplementation( usa, 1, SHORT_SIZE );
             array = usa;
         }
         break;
     case ID_INT_ARRAY:
         {
             osg::IntArray* ia = new osg::IntArray;
-            readArrayImplementation( ia, INT_SIZE, true );
+            readArrayImplementation( ia, 1, INT_SIZE );
             array = ia;
         }
         break;
     case ID_UINT_ARRAY:
         {
             osg::UIntArray* uia = new osg::UIntArray;
-            readArrayImplementation( uia, INT_SIZE, true );
+            readArrayImplementation( uia, 1, INT_SIZE );
             array = uia;
         }
         break;
     case ID_FLOAT_ARRAY:
         {
             osg::FloatArray* fa = new osg::FloatArray;
-            readArrayImplementation( fa, FLOAT_SIZE, true );
+            readArrayImplementation( fa, 1, FLOAT_SIZE );
             array = fa;
         }
         break;
     case ID_DOUBLE_ARRAY:
         {
             osg::DoubleArray* da = new osg::DoubleArray;
-            readArrayImplementation( da, DOUBLE_SIZE, true );
+            readArrayImplementation( da, 1, DOUBLE_SIZE );
             array = da;
         }
         break;
     case ID_VEC2B_ARRAY:
         {
             osg::Vec2bArray* va = new osg::Vec2bArray;
-            readArrayImplementation( va, 2*CHAR_SIZE );
+            readArrayImplementation( va, 2, CHAR_SIZE );
             array = va;
         }
         break;
     case ID_VEC3B_ARRAY:
         {
             osg::Vec3bArray* va = new osg::Vec3bArray;
-            readArrayImplementation( va, 3*CHAR_SIZE );
+            readArrayImplementation( va, 3, CHAR_SIZE );
             array = va;
         }
         break;
     case ID_VEC4B_ARRAY:
         {
             osg::Vec4bArray* va = new osg::Vec4bArray;
-            readArrayImplementation( va, 4*CHAR_SIZE );
+            readArrayImplementation( va, 4, CHAR_SIZE );
             array = va;
         }
         break;
     case ID_VEC4UB_ARRAY:
         {
             osg::Vec4ubArray* va = new osg::Vec4ubArray;
-            readArrayImplementation( va, 4*CHAR_SIZE );
+            readArrayImplementation( va, 4, CHAR_SIZE );
             array = va;
         }
         break;
     case ID_VEC2S_ARRAY:
         {
             osg::Vec2sArray* va = new osg::Vec2sArray;
-            readArrayImplementation( va, 2*SHORT_SIZE );
+            readArrayImplementation( va, 2, SHORT_SIZE );
             array = va;
         }
         break;
     case ID_VEC3S_ARRAY:
         {
             osg::Vec3sArray* va = new osg::Vec3sArray;
-            readArrayImplementation( va, 3*SHORT_SIZE );
+            readArrayImplementation( va, 3, SHORT_SIZE );
             array = va;
         }
         break;
     case ID_VEC4S_ARRAY:
         {
             osg::Vec4sArray* va = new osg::Vec4sArray;
-            readArrayImplementation( va, 4*SHORT_SIZE );
+            readArrayImplementation( va, 4, SHORT_SIZE );
             array = va;
         }
         break;
     case ID_VEC2_ARRAY:
         {
             osg::Vec2Array* va = new osg::Vec2Array;
-            readArrayImplementation( va, 2*FLOAT_SIZE );
+            readArrayImplementation( va, 2, FLOAT_SIZE );
             array = va;
         }
         break;
     case ID_VEC3_ARRAY:
         {
             osg::Vec3Array* va = new osg::Vec3Array;
-            readArrayImplementation( va, 3*FLOAT_SIZE );
+            readArrayImplementation( va, 3, FLOAT_SIZE );
             array = va;
         }
         break;
     case ID_VEC4_ARRAY:
         {
             osg::Vec4Array* va = new osg::Vec4Array;
-            readArrayImplementation( va, 4*FLOAT_SIZE );
+            readArrayImplementation( va, 4, FLOAT_SIZE );
             array = va;
         }
         break;
     case ID_VEC2D_ARRAY:
         {
             osg::Vec2dArray* va = new osg::Vec2dArray;
-            readArrayImplementation( va, 2*DOUBLE_SIZE );
+            readArrayImplementation( va, 2, DOUBLE_SIZE );
             array = va;
         }
         break;
     case ID_VEC3D_ARRAY:
         {
             osg::Vec3dArray* va = new osg::Vec3dArray;
-            readArrayImplementation( va, 3*DOUBLE_SIZE );
+            readArrayImplementation( va, 3, DOUBLE_SIZE );
             array = va;
         }
         break;
     case ID_VEC4D_ARRAY:
         {
             osg::Vec4dArray* va = new osg::Vec4dArray;
-            readArrayImplementation( va, 4*DOUBLE_SIZE );
+            readArrayImplementation( va, 4, DOUBLE_SIZE );
             array = va;
         }
         break;
@@ -683,9 +683,6 @@
 {
     _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;
@@ -806,7 +803,7 @@
 }

 template<typename T>
-void InputStream::readArrayImplementation( T* a, int read_size, bool
useByteSwap )
+void InputStream::readArrayImplementation( T* a, unsigned int
numComponentsPerElements, unsigned int componentSizeInBytes )
 {
     int size = 0;
     *this >> size >> BEGIN_BRACKET;
@@ -815,12 +812,8 @@
         a->resize( size );
         if ( isBinary() )
         {
-            readCharArray( (char*)&((*a)[0]), read_size*size ); checkStream();
-            if ( useByteSwap && _byteSwap )
-            {
-                for ( int i=0; i<size; ++i )
-                    osg::swapBytes( (char*)&((*a)[i]), read_size );
-            }
+            readComponentArray( (char*)&((*a)[0]), size,
numComponentsPerElements, componentSizeInBytes );
+            checkStream();
         }
         else
         {
Index: src/osgDB/StreamOperator.cpp
===================================================================
--- src/osgDB/StreamOperator.cpp        (revision 12975)
+++ src/osgDB/StreamOperator.cpp        (working copy)
@@ -16,6 +16,28 @@

 using namespace osgDB;

+void InputIterator::readComponentArray( char* s, unsigned int
numElements, unsigned int numComponentsPerElements, unsigned int
componentSizeInBytes)
+{
+    unsigned int size = numElements * numComponentsPerElements *
componentSizeInBytes;
+    if ( size>0 )
+    {
+        readCharArray( s, size);
+
+        if (_byteSwap && componentSizeInBytes>1)
+        {
+            char* ptr = s;
+            for(unsigned int i=0; i<numElements; ++i)
+            {
+                for(unsigned int j=0; j<numComponentsPerElements; ++j)
+                {
+                    osg::swapBytes( ptr, componentSizeInBytes );
+                    ptr += componentSizeInBytes;
+                }
+            }
+        }
+    }
+}
+
 void InputIterator::throwException( const std::string& msg )
 {
     if (_inputStream) _inputStream->throwException(msg);
_______________________________________________
osg-submissions mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-submissions-openscenegraph.org

Reply via email to