/* osgVisual test. distortionNG, experimental. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ #include "ReaderWriterDist.h" #include ReaderWriterDist::ReaderWriterDist() { supportsExtension( "dist", "distortion set loader"); extensionToAdd = ".dist"; // Configure Compression and instantiate read/write-options bool asAscii = true; bool compressionEnabled = false; std::string readOptionString = ""; std::string writeOptionString = ""; if(asAscii) { readOptionString = "Ascii"; writeOptionString = "Ascii"; } if (compressionEnabled) writeOptionString+=" Compressor=zlib"; readOptions = new osgDB::Options( readOptionString.c_str() ); writeOptions = new osgDB::Options( writeOptionString.c_str() ); // Get ReaderWriter rw = osgDB::Registry::instance()->getReaderWriterForExtension("osgt"); } ReaderWriterDist::Options* ReaderWriterDist::prepareReading( ReadResult& result, std::string& fileName, std::ios::openmode& mode, const Options* options ) const { std::string ext = osgDB::getLowerCaseFileExtension( fileName ); if ( !acceptsExtension(ext) ) { result = ReadResult::FILE_NOT_HANDLED; return 0; } fileName = osgDB::findDataFile( fileName, options ); if ( fileName.empty() ) { result = ReadResult::FILE_NOT_FOUND; return 0; } osg::ref_ptr local_opt = options ? static_cast(options->clone(osg::CopyOp::SHALLOW_COPY)) : new Options; local_opt->getDatabasePathList().push_front(osgDB::getFilePath(fileName)); mode |= std::ios::binary; return local_opt.release(); } ReaderWriterDist::ReadResult ReaderWriterDist::readObject( const std::string& file, const Options* options) const { // This function must be reimplemented because the osgb|t|x plugin would test for a valid filename extension and thus would block the fiel due to it's .dist extension. ReadResult result = ReadResult::FILE_LOADED; std::string fileName = file; std::ios::openmode mode = std::ios::in; Options* local_opt = prepareReading( result, fileName, mode, options ); if ( !result.success() ) return result; osgDB::ifstream istream( fileName.c_str(), mode ); return readObject( istream, local_opt ); } ReaderWriterDist::ReadResult ReaderWriterDist::readObject( std::istream& fin, const Options* options ) const { // This function uses the osgb|t|x plugin to perform the operation if ( rw ) { osgDB::ReaderWriter::ReadResult rr = rw->readObject( fin, readOptions ); if (rr.success()) { return( rr.takeObject() ); } else OSG_NOTIFY( osg::WARN ) << "ERROR: Unable to convert stream to node" << std::endl; } OSG_NOTIFY( osg::WARN ) << "ERROR: Unable to get ReaderWriter for internally used extension" << std::endl; return( ReadResult::NOT_IMPLEMENTED ); } ReaderWriterDist::Options* ReaderWriterDist::prepareWriting( WriteResult& result, const std::string& fileName, std::ios::openmode& mode, const Options* options ) const { std::string ext = osgDB::getFileExtension( fileName ); if ( !acceptsExtension(ext) ) result = WriteResult::FILE_NOT_HANDLED; osg::ref_ptr local_opt = options ? static_cast(options->clone(osg::CopyOp::SHALLOW_COPY)) : new Options; local_opt->getDatabasePathList().push_front(osgDB::getFilePath(fileName)); mode |= std::ios::binary; return local_opt.release(); } ReaderWriterDist::WriteResult ReaderWriterDist::writeObject( const osg::Object& object, const std::string& fileName, const osgDB::ReaderWriter::Options* options ) const { // This function must be reimplemented because the osgb|t|x plugin would test for a valid filename extension and thus would block the fiel due to it's .dist extension. WriteResult result = WriteResult::FILE_SAVED; std::ios::openmode mode = std::ios::out; osg::ref_ptr local_opt = prepareWriting( result, fileName, mode, options ); if ( !result.success() ) return result; osgDB::ofstream fout( fileName.c_str(), mode ); if ( !fout ) return WriteResult::ERROR_IN_WRITING_FILE; result = writeObject( object, fout, local_opt.get() ); fout.close(); return result; } ReaderWriterDist::WriteResult ReaderWriterDist::writeObject( const osg::Object& object, std::ostream& fout, const osgDB::ReaderWriter::Options* options ) const { // This function uses the osgb|t|x plugin to perform the operation if ( rw ) { osgDB::ReaderWriter::WriteResult wr = rw->writeObject( object, fout, writeOptions ); if (wr.success() ) { OSG_NOTIFY( osg::WARN ) << "schreiben geschafft!"<< std::endl; } else OSG_NOTIFY( osg::WARN ) << "ERROR: Save failed: " << wr.message() << std::endl; return(wr); } OSG_NOTIFY( osg::WARN ) << "ERROR: Unable to get ReaderWriter for internally used extension" << std::endl; return( WriteResult::NOT_IMPLEMENTED ); } // Add ourself to the Registry to instantiate the reader/writer. REGISTER_OSGPLUGIN(dist, ReaderWriterDist)