From 2e38e4751c89e43e1c64c6ab40a9f371ae3d96fe Mon Sep 17 00:00:00 2001 From: Mathieu MARACHE Date: Fri, 7 Sep 2018 14:04:41 +0200 Subject: [PATCH] Added saving of inline Images without having the physical image file. The can be read from and inlined image file --- src/osgDB/OutputStream.cpp | 69 ++++++++++++++++++++++++++------------ 1 file changed, 48 insertions(+), 21 deletions(-) diff --git a/src/osgDB/OutputStream.cpp b/src/osgDB/OutputStream.cpp index 29a29e6f3..c5a385f81 100644 --- a/src/osgDB/OutputStream.cpp +++ b/src/osgDB/OutputStream.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -581,35 +582,61 @@ void OutputStream::writeImage( const osg::Image* img ) if ( isBinary() ) { std::string fullPath = osgDB::findDataFile( img->getFileName(), _options.get() ); - osgDB::ifstream infile( fullPath.c_str(), std::ios::in|std::ios::binary ); - if ( infile ) + if (fullPath.empty()==false) { - infile.seekg( 0, std::ios::end ); - unsigned int size = infile.tellg(); - writeSize(size); - - if ( size>0 ) + osgDB::ifstream infile( fullPath.c_str(), std::ios::in|std::ios::binary ); + + if ( infile ) { - char* data = new char[size]; - if ( !data ) - { - throwException( "OutputStream::writeImage(): Out of memory." ); - if ( getException() ) return; - } + infile.seekg( 0, std::ios::end ); + unsigned int size = infile.tellg(); + writeSize(size); - infile.seekg( 0, std::ios::beg ); - infile.read( data, size ); - writeCharArray( data, size ); - delete[] data; + if ( size>0 ) + { + char* data = new char[size]; + if ( !data ) + { + throwException( "OutputStream::writeImage(): Out of memory." ); + if ( getException() ) return; + } + + infile.seekg( 0, std::ios::beg ); + infile.read( data, size ); + writeCharArray( data, size ); + delete[] data; + } + infile.close(); + } + else + { + OSG_WARN << "OutputStream::writeImage(): Failed to open image file " + << img->getFileName() << std::endl; + *this << (unsigned int)0; } - infile.close(); } else { - OSG_WARN << "OutputStream::writeImage(): Failed to open image file " - << img->getFileName() << std::endl; - *this << (unsigned int)0; + std::string ext = osgDB::getFileExtension( img->getFileName() ); + osgDB::ReaderWriter* writer = + osgDB::Registry::instance()->getReaderWriterForExtension( ext ); + if ( writer ) + { + std::stringstream outputStream; + writer->writeImage(*img, outputStream, getOptions()); + std::string data (outputStream.str()); + unsigned int size = data.size(); + writeSize(size); + writeCharArray( data.c_str(), size ); + } + else + { + OSG_WARN << "OutputStream::writeImage(): Failed to find a plugin to write the image file " + << img->getFileName() << std::endl; + *this << (unsigned int)0; + } } + } break; case IMAGE_EXTERNAL: