Refactored the handling of readImage/writeImage/readObject/writeObject to avoid double setting of unique ID's, fixing the problem in reading/writing files with images

This commit is contained in:
Robert Osfield 2010-09-23 16:12:05 +00:00
parent 915b38dc25
commit d9a133476a
4 changed files with 194 additions and 152 deletions

View File

@ -137,6 +137,7 @@ public:
osg::PrimitiveSet* readPrimitiveSet(); osg::PrimitiveSet* readPrimitiveSet();
osg::Image* readImage(); osg::Image* readImage();
osg::Object* readObject( osg::Object* existingObj=0 ); osg::Object* readObject( osg::Object* existingObj=0 );
osg::Object* readObjectFields( const std::string& className, osg::Object* existingObj=0);
ReadType start( InputIterator* ); ReadType start( InputIterator* );
void decompress(); void decompress();

View File

@ -146,6 +146,7 @@ public:
void writePrimitiveSet( const osg::PrimitiveSet* p ); void writePrimitiveSet( const osg::PrimitiveSet* p );
void writeImage( const osg::Image* img ); void writeImage( const osg::Image* img );
void writeObject( const osg::Object* obj ); void writeObject( const osg::Object* obj );
void writeObjectFields( const osg::Object* obj );
void start( OutputIterator* outIterator, WriteType type ); void start( OutputIterator* outIterator, WriteType type );
void compress( std::ostream* ostream ); void compress( std::ostream* ostream );
@ -161,8 +162,8 @@ protected:
template<typename T> template<typename T>
void writeArrayImplementation( const T*, int write_size, unsigned int numInRow=1 ); void writeArrayImplementation( const T*, int write_size, unsigned int numInRow=1 );
unsigned int findOrCreateArrayID( const osg::Array* array ); unsigned int findOrCreateArrayID( const osg::Array* array, bool& newID );
unsigned int findOrCreateObjectID( const osg::Object* obj ); unsigned int findOrCreateObjectID( const osg::Object* obj, bool& newID );
ArrayMap _arrayMap; ArrayMap _arrayMap;
ObjectMap _objectMap; ObjectMap _objectMap;

View File

@ -171,7 +171,10 @@ osg::Array* InputStream::readArray()
*this >> PROPERTY("ArrayID") >> id; *this >> PROPERTY("ArrayID") >> id;
ArrayMap::iterator itr = _arrayMap.find( id ); ArrayMap::iterator itr = _arrayMap.find( id );
if ( itr!=_arrayMap.end() ) return itr->second.get(); if ( itr!=_arrayMap.end() )
{
return itr->second.get();
}
DEF_MAPPEE(ArrayType, type); DEF_MAPPEE(ArrayType, type);
*this >> type; *this >> type;
@ -330,6 +333,7 @@ osg::Array* InputStream::readArray()
if ( getException() ) return NULL; if ( getException() ) return NULL;
_arrayMap[id] = array; _arrayMap[id] = array;
return array.release(); return array.release();
} }
@ -417,14 +421,15 @@ osg::PrimitiveSet* InputStream::readPrimitiveSet()
osg::Image* InputStream::readImage() osg::Image* InputStream::readImage()
{ {
std::string className="osg::Image";
unsigned int id = 0; unsigned int id = 0;
*this >> PROPERTY("ImageID") >> id;
*this >> PROPERTY("UniqueID") >> id;
if ( getException() ) return NULL; if ( getException() ) return NULL;
IdentifierMap::iterator itr = _identifierMap.find( id ); IdentifierMap::iterator itr = _identifierMap.find( id );
if ( itr!=_identifierMap.end() ) if ( itr!=_identifierMap.end() )
{ {
advanceToCurrentEndBracket();
return static_cast<osg::Image*>( itr->second.get() ); return static_cast<osg::Image*>( itr->second.get() );
} }
@ -534,7 +539,10 @@ osg::Image* InputStream::readImage()
image->setWriteHint( (osg::Image::WriteHint)writeHint ); image->setWriteHint( (osg::Image::WriteHint)writeHint );
} }
image = static_cast<osg::Image*>( readObject(image.get()) ); image = static_cast<osg::Image*>( readObjectFields(className, image.get()) );
_identifierMap[id] = image;
return image.release(); return image.release();
} }
@ -552,12 +560,22 @@ osg::Object* InputStream::readObject( osg::Object* existingObj )
return itr->second.get(); return itr->second.get();
} }
osg::ref_ptr<osg::Object> obj = readObjectFields( className );
_identifierMap[id] = obj;
advanceToCurrentEndBracket();
return obj.release();
}
osg::Object* InputStream::readObjectFields( const std::string& className, osg::Object* existingObj )
{
ObjectWrapper* wrapper = Registry::instance()->getObjectWrapperManager()->findWrapper( className ); ObjectWrapper* wrapper = Registry::instance()->getObjectWrapperManager()->findWrapper( className );
if ( !wrapper ) if ( !wrapper )
{ {
OSG_WARN << "InputStream::readObject(): Unsupported wrapper class " OSG_WARN << "InputStream::readObject(): Unsupported wrapper class "
<< className << std::endl; << className << std::endl;
advanceToCurrentEndBracket();
return NULL; return NULL;
} }
_fields.push_back( className ); _fields.push_back( className );
@ -565,8 +583,6 @@ osg::Object* InputStream::readObject( osg::Object* existingObj )
osg::ref_ptr<osg::Object> obj = existingObj ? existingObj : wrapper->getProto()->cloneType(); osg::ref_ptr<osg::Object> obj = existingObj ? existingObj : wrapper->getProto()->cloneType();
if ( obj.valid() ) if ( obj.valid() )
{ {
_identifierMap[id] = obj;
const StringList& associates = wrapper->getAssociates(); const StringList& associates = wrapper->getAssociates();
for ( StringList::const_iterator itr=associates.begin(); itr!=associates.end(); ++itr ) for ( StringList::const_iterator itr=associates.begin(); itr!=associates.end(); ++itr )
{ {
@ -585,7 +601,6 @@ osg::Object* InputStream::readObject( osg::Object* existingObj )
_fields.pop_back(); _fields.pop_back();
} }
} }
advanceToCurrentEndBracket();
_fields.pop_back(); _fields.pop_back();
return obj.release(); return obj.release();
} }

View File

@ -140,10 +140,10 @@ void OutputStream::writeArray( const osg::Array* a )
{ {
if ( !a ) return; if ( !a ) return;
size_t oldSize = _arrayMap.size(); bool newID = false;
unsigned int id = findOrCreateArrayID( a ); unsigned int id = findOrCreateArrayID( a, newID );
*this << PROPERTY("ArrayID") << id; *this << PROPERTY("ArrayID") << id;
if ( id<=oldSize ) // Shared array if ( !newID ) // Shared array
{ {
*this << std::endl; *this << std::endl;
return; return;
@ -295,10 +295,18 @@ void OutputStream::writeImage( const osg::Image* img )
{ {
if ( !img ) return; if ( !img ) return;
unsigned int id = findOrCreateObjectID( img ); // std::string name = img->libraryName();
*this << PROPERTY("ImageID") << id << std::endl; // Write image ID // name += std::string("::") + img->className();
bool newID = false;
unsigned int id = findOrCreateObjectID( img, newID );
// *this << name << BEGIN_BRACKET << std::endl; // Write object name
*this << PROPERTY("UniqueID") << id << std::endl; // Write image ID
if ( getException() ) return; if ( getException() ) return;
if (newID)
{
*this << PROPERTY("FileName"); writeWrappedString(img->getFileName()); *this << std::endl; *this << PROPERTY("FileName"); writeWrappedString(img->getFileName()); *this << std::endl;
*this << PROPERTY("WriteHint") << (int)img->getWriteHint(); *this << PROPERTY("WriteHint") << (int)img->getWriteHint();
if ( getException() ) return; if ( getException() ) return;
@ -396,7 +404,11 @@ void OutputStream::writeImage( const osg::Image* img )
default: default:
break; break;
} }
writeObject( img );
writeObjectFields( img );
}
// *this << END_BRACKET << std::endl;
} }
void OutputStream::writeObject( const osg::Object* obj ) void OutputStream::writeObject( const osg::Object* obj )
@ -405,22 +417,32 @@ void OutputStream::writeObject( const osg::Object* obj )
std::string name = obj->libraryName(); std::string name = obj->libraryName();
name += std::string("::") + obj->className(); name += std::string("::") + obj->className();
size_t oldSize = _objectMap.size();
unsigned int id = findOrCreateObjectID( obj ); bool newID = false;
unsigned int id = findOrCreateObjectID( obj, newID );
*this << name << BEGIN_BRACKET << std::endl; // Write object name *this << name << BEGIN_BRACKET << std::endl; // Write object name
*this << PROPERTY("UniqueID") << id << std::endl; // Write object ID *this << PROPERTY("UniqueID") << id << std::endl; // Write object ID
if ( getException() ) return; if ( getException() ) return;
// Check whether this is a shared object or not if (newID)
if ( id>oldSize )
{ {
writeObjectFields(obj);
}
*this << END_BRACKET << std::endl;
}
void OutputStream::writeObjectFields( const osg::Object* obj )
{
std::string name = obj->libraryName();
name += std::string("::") + obj->className();
ObjectWrapper* wrapper = Registry::instance()->getObjectWrapperManager()->findWrapper( name ); ObjectWrapper* wrapper = Registry::instance()->getObjectWrapperManager()->findWrapper( name );
if ( !wrapper ) if ( !wrapper )
{ {
OSG_WARN << "OutputStream::writeObject(): Unsupported wrapper class " OSG_WARN << "OutputStream::writeObject(): Unsupported wrapper class "
<< name << std::endl; << name << std::endl;
*this << END_BRACKET << std::endl;
return; return;
} }
_fields.push_back( name ); _fields.push_back( name );
@ -462,8 +484,7 @@ void OutputStream::writeObject( const osg::Object* obj )
_fields.pop_back(); _fields.pop_back();
} }
_fields.pop_back(); _fields.pop_back();
}
*this << END_BRACKET << std::endl;
} }
void OutputStream::start( OutputIterator* outIterator, OutputStream::WriteType type ) void OutputStream::start( OutputIterator* outIterator, OutputStream::WriteType type )
@ -635,26 +656,30 @@ void OutputStream::writeArrayImplementation( const T* a, int write_size, unsigne
*this << END_BRACKET << std::endl; *this << END_BRACKET << std::endl;
} }
unsigned int OutputStream::findOrCreateArrayID( const osg::Array* array ) unsigned int OutputStream::findOrCreateArrayID( const osg::Array* array, bool& newID )
{ {
ArrayMap::iterator itr = _arrayMap.find( array ); ArrayMap::iterator itr = _arrayMap.find( array );
if ( itr==_arrayMap.end() ) if ( itr==_arrayMap.end() )
{ {
unsigned int id = _arrayMap.size()+1; unsigned int id = _arrayMap.size()+1;
_arrayMap[array] = id; _arrayMap[array] = id;
newID = true;
return id; return id;
} }
newID = false;
return itr->second; return itr->second;
} }
unsigned int OutputStream::findOrCreateObjectID( const osg::Object* obj ) unsigned int OutputStream::findOrCreateObjectID( const osg::Object* obj, bool& newID )
{ {
ObjectMap::iterator itr = _objectMap.find( obj ); ObjectMap::iterator itr = _objectMap.find( obj );
if ( itr==_objectMap.end() ) if ( itr==_objectMap.end() )
{ {
unsigned int id = _objectMap.size()+1; unsigned int id = _objectMap.size()+1;
_objectMap[obj] = id; _objectMap[obj] = id;
newID = true;
return id; return id;
} }
newID = false;
return itr->second; return itr->second;
} }