diff --git a/src/osgPlugins/gz/CMakeLists.txt b/src/osgPlugins/gz/CMakeLists.txt new file mode 100644 index 000000000..6e14d6d3b --- /dev/null +++ b/src/osgPlugins/gz/CMakeLists.txt @@ -0,0 +1,12 @@ +#this file is automatically generated +INCLUDE_DIRECTORIES( ${ZLIB_INCLUDE_DIRS} ) + +SET(TARGET_SRC + ReaderWriterGZ.cpp +) + +SET(TARGET_LIBRARIES_VARS ZLIB_LIBRARY ) + + +#### end var setup ### +SETUP_PLUGIN(gz) diff --git a/src/osgPlugins/gz/ReaderWriterGZ.cpp b/src/osgPlugins/gz/ReaderWriterGZ.cpp new file mode 100644 index 000000000..f1fdc9ee0 --- /dev/null +++ b/src/osgPlugins/gz/ReaderWriterGZ.cpp @@ -0,0 +1,275 @@ +/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2008 Robert Osfield + * + * This library is open source and may be redistributed and/or modified under + * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or + * (at your option) any later version. The full license is in LICENSE file + * included with this distribution, and on the openscenegraph.org website. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * OpenSceneGraph Public License for more details. +*/ + +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include + +/////////////////////////////////////////////////////////////////////////////////////////////////// +// +// ReaderWriterGZ +// +class ReaderWriterGZ : public osgDB::ReaderWriter +{ + public: + + enum ObjectType + { + OBJECT, + ARCHIVE, + IMAGE, + HEIGHTFIELD, + NODE + }; + + ReaderWriterGZ(); + + ~ReaderWriterGZ(); + + virtual const char* className() const { return "HTTP Protocol Model Reader"; } + + virtual ReadResult openArchive(const std::string& fileName,ArchiveStatus status, unsigned int , const Options* options) const + { + if (status!=READ) return ReadResult(ReadResult::FILE_NOT_HANDLED); + else return readFile(ARCHIVE,fileName,options); + } + + virtual ReadResult readObject(const std::string& fileName, const osgDB::ReaderWriter::Options* options) const + { + return readFile(OBJECT,fileName,options); + } + + virtual ReadResult readImage(const std::string& fileName, const osgDB::ReaderWriter::Options* options) const + { + return readFile(IMAGE,fileName,options); + } + + virtual ReadResult readHeightField(const std::string& fileName, const osgDB::ReaderWriter::Options* options) const + { + return readFile(HEIGHTFIELD,fileName,options); + } + + virtual ReadResult readNode(const std::string& fileName, const osgDB::ReaderWriter::Options* options) const + { + return readFile(NODE,fileName,options); + } + + ReadResult readFile(ObjectType objectType, osgDB::ReaderWriter* rw, std::istream& fin, const osgDB::ReaderWriter::Options* options) const; + + ReadResult readFile(ObjectType objectType, const std::string& fullFileName, const osgDB::ReaderWriter::Options* options) const; + + + + virtual WriteResult writeObject(const osg::Object& obj, const std::string& fileName, const osgDB::ReaderWriter::Options* options) const + { + return writeFile(OBJECT, &obj, fileName, options); + } + + virtual WriteResult writeImage(const osg::Image& image, const std::string& fileName, const osgDB::ReaderWriter::Options* options) const + { + return writeFile(IMAGE, &image, fileName, options); + } + + virtual WriteResult writeHeightField(const osg::HeightField& hf, const std::string& fileName, const osgDB::ReaderWriter::Options* options) const + { + return writeFile(HEIGHTFIELD, &hf, fileName, options); + } + + virtual WriteResult writeNode(const osg::Node& node, const std::string& fileName, const osgDB::ReaderWriter::Options* options) const + { + return writeFile(NODE, &node, fileName,options); + } + + WriteResult writeFile(ObjectType objectType, const osg::Object* object, osgDB::ReaderWriter* rw, std::ostream& fin, const osgDB::ReaderWriter::Options* options) const; + + WriteResult writeFile(ObjectType objectType, const osg::Object* object, const std::string& fullFileName, const osgDB::ReaderWriter::Options* options) const; + +}; + +ReaderWriterGZ::ReaderWriterGZ() +{ + // osg::notify(osg::NOTICE)<<"ReaderWriterGZ::ReaderWriterGZ()"<readObject(fin,options); + case(ARCHIVE): return rw->openArchive(fin,options); + case(IMAGE): return rw->readImage(fin,options); + case(HEIGHTFIELD): return rw->readHeightField(fin,options); + case(NODE): return rw->readNode(fin,options); + default: break; + } + return ReadResult::FILE_NOT_HANDLED; +} + +osgDB::ReaderWriter::ReadResult ReaderWriterGZ::readFile(ObjectType objectType, const std::string& fullFileName, const osgDB::ReaderWriter::Options *options) const +{ + std::string ext = osgDB::getLowerCaseFileExtension(fullFileName); + if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED; + + osgDB::ReaderWriter* rw = 0; + + if (osgDB::equalCaseInsensitive(ext,"osgz")) + { + rw = osgDB::Registry::instance()->getReaderWriterForExtension("osg"); + osg::notify(osg::NOTICE)<<"osgz ReaderWriter "<getReaderWriterForExtension("ive"); + osg::notify(osg::NOTICE)<<"ivez ReaderWriter "<getReaderWriterForExtension(baseExt); + osg::notify(osg::NOTICE)< local_opt = options ? static_cast(options->clone(osg::CopyOp::SHALLOW_COPY)) : new Options; + local_opt->getDatabasePathList().push_front(osgDB::getFilePath(fileName)); + + std::ifstream fin(fileName.c_str()); + if (!fin) return ReadResult::ERROR_IN_READING_FILE; + + std::streampos start = fin.tellg(); + fin.seekg(0, std::ios::end); + std::streampos end = fin.tellg(); + fin.seekg(start, std::ios::beg); + std::streampos sourceLen = end - start; + + /* empty stream into memory, open that, and keep the pointer in a FreeTypeFont for cleanup */ + char* buffer = new char[sourceLen]; + fin.read(reinterpret_cast(buffer), sourceLen); + if (!fin || (static_cast(fin.gcount()) != sourceLen)) + { + osg::notify(osg::WARN)<<" .... the compressed file could not be read from its stream"<writeObject(*object, fout, options); + case(IMAGE): return rw->writeImage(*(dynamic_cast(object)), fout, options); + case(HEIGHTFIELD): return rw->writeHeightField(*(dynamic_cast(object)), fout, options); + case(NODE): return rw->writeNode(*(dynamic_cast(object)), fout,options); + default: break; + } + return WriteResult::FILE_NOT_HANDLED; +} + +osgDB::ReaderWriter::WriteResult ReaderWriterGZ::writeFile(ObjectType objectType, const osg::Object* object, const std::string& fullFileName, const osgDB::ReaderWriter::Options *options) const +{ + std::string ext = osgDB::getLowerCaseFileExtension(fullFileName); + if (!acceptsExtension(ext)) return WriteResult::FILE_NOT_HANDLED; + + osgDB::ReaderWriter* rw = 0; + + if (osgDB::equalCaseInsensitive(ext,"osgz")) + { + rw = osgDB::Registry::instance()->getReaderWriterForExtension("osg"); + osg::notify(osg::NOTICE)<<"osgz ReaderWriter "<getReaderWriterForExtension("ive"); + osg::notify(osg::NOTICE)<<"ivez ReaderWriter "<getReaderWriterForExtension(baseExt); + osg::notify(osg::NOTICE)<