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:
parent
915b38dc25
commit
d9a133476a
@ -137,6 +137,7 @@ public:
|
||||
osg::PrimitiveSet* readPrimitiveSet();
|
||||
osg::Image* readImage();
|
||||
osg::Object* readObject( osg::Object* existingObj=0 );
|
||||
osg::Object* readObjectFields( const std::string& className, osg::Object* existingObj=0);
|
||||
|
||||
ReadType start( InputIterator* );
|
||||
void decompress();
|
||||
|
@ -146,6 +146,7 @@ public:
|
||||
void writePrimitiveSet( const osg::PrimitiveSet* p );
|
||||
void writeImage( const osg::Image* img );
|
||||
void writeObject( const osg::Object* obj );
|
||||
void writeObjectFields( const osg::Object* obj );
|
||||
|
||||
void start( OutputIterator* outIterator, WriteType type );
|
||||
void compress( std::ostream* ostream );
|
||||
@ -161,8 +162,8 @@ protected:
|
||||
template<typename T>
|
||||
void writeArrayImplementation( const T*, int write_size, unsigned int numInRow=1 );
|
||||
|
||||
unsigned int findOrCreateArrayID( const osg::Array* array );
|
||||
unsigned int findOrCreateObjectID( const osg::Object* obj );
|
||||
unsigned int findOrCreateArrayID( const osg::Array* array, bool& newID );
|
||||
unsigned int findOrCreateObjectID( const osg::Object* obj, bool& newID );
|
||||
|
||||
ArrayMap _arrayMap;
|
||||
ObjectMap _objectMap;
|
||||
|
@ -171,7 +171,10 @@ osg::Array* InputStream::readArray()
|
||||
*this >> PROPERTY("ArrayID") >> 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);
|
||||
*this >> type;
|
||||
@ -330,6 +333,7 @@ osg::Array* InputStream::readArray()
|
||||
|
||||
if ( getException() ) return NULL;
|
||||
_arrayMap[id] = array;
|
||||
|
||||
return array.release();
|
||||
}
|
||||
|
||||
@ -417,14 +421,15 @@ osg::PrimitiveSet* InputStream::readPrimitiveSet()
|
||||
|
||||
osg::Image* InputStream::readImage()
|
||||
{
|
||||
std::string className="osg::Image";
|
||||
unsigned int id = 0;
|
||||
*this >> PROPERTY("ImageID") >> id;
|
||||
|
||||
*this >> PROPERTY("UniqueID") >> id;
|
||||
if ( getException() ) return NULL;
|
||||
|
||||
IdentifierMap::iterator itr = _identifierMap.find( id );
|
||||
if ( itr!=_identifierMap.end() )
|
||||
{
|
||||
advanceToCurrentEndBracket();
|
||||
return static_cast<osg::Image*>( itr->second.get() );
|
||||
}
|
||||
|
||||
@ -534,7 +539,10 @@ osg::Image* InputStream::readImage()
|
||||
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();
|
||||
}
|
||||
|
||||
@ -552,12 +560,22 @@ osg::Object* InputStream::readObject( osg::Object* existingObj )
|
||||
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 );
|
||||
if ( !wrapper )
|
||||
{
|
||||
OSG_WARN << "InputStream::readObject(): Unsupported wrapper class "
|
||||
<< className << std::endl;
|
||||
advanceToCurrentEndBracket();
|
||||
return NULL;
|
||||
}
|
||||
_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();
|
||||
if ( obj.valid() )
|
||||
{
|
||||
_identifierMap[id] = obj;
|
||||
|
||||
const StringList& associates = wrapper->getAssociates();
|
||||
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();
|
||||
}
|
||||
}
|
||||
advanceToCurrentEndBracket();
|
||||
_fields.pop_back();
|
||||
return obj.release();
|
||||
}
|
||||
|
@ -140,10 +140,10 @@ void OutputStream::writeArray( const osg::Array* a )
|
||||
{
|
||||
if ( !a ) return;
|
||||
|
||||
size_t oldSize = _arrayMap.size();
|
||||
unsigned int id = findOrCreateArrayID( a );
|
||||
bool newID = false;
|
||||
unsigned int id = findOrCreateArrayID( a, newID );
|
||||
*this << PROPERTY("ArrayID") << id;
|
||||
if ( id<=oldSize ) // Shared array
|
||||
if ( !newID ) // Shared array
|
||||
{
|
||||
*this << std::endl;
|
||||
return;
|
||||
@ -295,10 +295,18 @@ void OutputStream::writeImage( const osg::Image* img )
|
||||
{
|
||||
if ( !img ) return;
|
||||
|
||||
unsigned int id = findOrCreateObjectID( img );
|
||||
*this << PROPERTY("ImageID") << id << std::endl; // Write image ID
|
||||
// std::string name = img->libraryName();
|
||||
// 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 (newID)
|
||||
{
|
||||
*this << PROPERTY("FileName"); writeWrappedString(img->getFileName()); *this << std::endl;
|
||||
*this << PROPERTY("WriteHint") << (int)img->getWriteHint();
|
||||
if ( getException() ) return;
|
||||
@ -396,7 +404,11 @@ void OutputStream::writeImage( const osg::Image* img )
|
||||
default:
|
||||
break;
|
||||
}
|
||||
writeObject( img );
|
||||
|
||||
writeObjectFields( img );
|
||||
}
|
||||
|
||||
// *this << END_BRACKET << std::endl;
|
||||
}
|
||||
|
||||
void OutputStream::writeObject( const osg::Object* obj )
|
||||
@ -405,22 +417,32 @@ void OutputStream::writeObject( const osg::Object* obj )
|
||||
|
||||
std::string name = obj->libraryName();
|
||||
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 << PROPERTY("UniqueID") << id << std::endl; // Write object ID
|
||||
if ( getException() ) return;
|
||||
|
||||
// Check whether this is a shared object or not
|
||||
if ( id>oldSize )
|
||||
if (newID)
|
||||
{
|
||||
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 );
|
||||
if ( !wrapper )
|
||||
{
|
||||
OSG_WARN << "OutputStream::writeObject(): Unsupported wrapper class "
|
||||
<< name << std::endl;
|
||||
*this << END_BRACKET << std::endl;
|
||||
return;
|
||||
}
|
||||
_fields.push_back( name );
|
||||
@ -462,8 +484,7 @@ void OutputStream::writeObject( const osg::Object* obj )
|
||||
_fields.pop_back();
|
||||
}
|
||||
_fields.pop_back();
|
||||
}
|
||||
*this << END_BRACKET << std::endl;
|
||||
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
unsigned int OutputStream::findOrCreateArrayID( const osg::Array* array )
|
||||
unsigned int OutputStream::findOrCreateArrayID( const osg::Array* array, bool& newID )
|
||||
{
|
||||
ArrayMap::iterator itr = _arrayMap.find( array );
|
||||
if ( itr==_arrayMap.end() )
|
||||
{
|
||||
unsigned int id = _arrayMap.size()+1;
|
||||
_arrayMap[array] = id;
|
||||
newID = true;
|
||||
return id;
|
||||
}
|
||||
newID = false;
|
||||
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 );
|
||||
if ( itr==_objectMap.end() )
|
||||
{
|
||||
unsigned int id = _objectMap.size()+1;
|
||||
_objectMap[obj] = id;
|
||||
newID = true;
|
||||
return id;
|
||||
}
|
||||
newID = false;
|
||||
return itr->second;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user