root/OpenSceneGraph/trunk/src/osgPlugins/ive/ReaderWriterIVE.cpp @ 10762

Revision 10762, 8.3 kB (checked in by robert, 4 years ago)

Converted .ive plugin from using C++ exceptions to using a local equivelant, enabling the build on embedded systems.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#include "MatrixTransform.h"
2#include "Group.h"
3
4#include <osg/Notify>
5
6#include <osgDB/FileNameUtils>
7#include <osgDB/FileUtils>
8#include <osgDB/fstream>
9#include <osgDB/Registry>
10
11using namespace osg;
12using namespace osgDB;
13
14class ReaderWriterIVE : public ReaderWriter
15{
16    public:
17   
18        ReaderWriterIVE()
19        {
20            supportsExtension("ive","OpenSceneGraph native binary format");
21
22            supportsOption("compressed","Export option, use zlib compression to compress the data in the .ive ");
23            supportsOption("noTexturesInIVEFile","Export option");
24            supportsOption("includeImageFileInIVEFile","Export option");
25            supportsOption("compressImageData","Export option");
26            supportsOption("inlineExternalReferencesInIVEFile","Export option");
27            supportsOption("noWriteExternalReferenceFiles","Export option");
28            supportsOption("useOriginalExternalReferences","Export option");
29            supportsOption("TerrainMaximumErrorToSizeRatio=value","Export option that controls error matric used to determine terrain HieghtField storage precision.");
30            supportsOption("noLoadExternalReferenceFiles","Import option");
31        }
32   
33        virtual const char* className() const { return "IVE Reader/Writer"; }
34
35        virtual bool acceptsExtension(const std::string& extension) const
36        {
37            return equalCaseInsensitive(extension,"ive");
38        }
39
40        virtual ReadResult readObject(const std::string& file, const Options* options) const
41        {
42            return readNode(file, options);
43        }
44
45        virtual ReadResult readImage(const std::string& file, const Options* options) const
46        {
47            std::string ext = osgDB::getLowerCaseFileExtension(file);
48            if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED;
49            std::string fileName = osgDB::findDataFile(file, options);
50            if (fileName.empty()) return ReadResult::FILE_NOT_FOUND;
51
52            // code for setting up the database path so that internally referenced files are searched for on relative paths.
53            osg::ref_ptr<Options> local_opt = options ? static_cast<Options*>(options->clone(osg::CopyOp::SHALLOW_COPY)) : new Options;
54            local_opt->getDatabasePathList().push_front(osgDB::getFilePath(fileName));
55
56            osgDB::ifstream istream(fileName.c_str(), std::ios::in | std::ios::binary);
57            return readImage(istream, local_opt.get());
58        }
59       
60        virtual ReadResult readNode(const std::string& file, const Options* options) const
61        {
62            std::string ext = osgDB::getLowerCaseFileExtension(file);
63            if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED;
64
65            std::string fileName = osgDB::findDataFile( file, options );
66            if (fileName.empty()) return ReadResult::FILE_NOT_FOUND;
67
68            // code for setting up the database path so that internally referenced file are searched for on relative paths.
69            osg::ref_ptr<Options> local_opt = options ? static_cast<Options*>(options->clone(osg::CopyOp::SHALLOW_COPY)) : new Options;
70            local_opt->getDatabasePathList().push_front(osgDB::getFilePath(fileName));
71           
72            osgDB::ifstream istream(fileName.c_str(), std::ios::in | std::ios::binary);
73            return readNode(istream,local_opt.get());
74        }
75       
76        virtual ReadResult readObject(std::istream& fin, const Options* options) const
77        {
78            return readNode(fin, options);
79        }
80
81        virtual ReadResult readImage(std::istream& fin, const Options* options) const
82        {
83            ive::DataInputStream in(&fin, options);
84            if (in.getException())
85            {
86                return in.getException()->getError();
87            }
88
89            return in.readImage(ive::IMAGE_INCLUDE_DATA);
90        }
91
92        virtual ReadResult readNode(std::istream& fin, const Options* options) const
93        {
94            // Create datainputstream.
95            ive::DataInputStream in(&fin, options);
96            if (in.getException())
97            {
98                return in.getException()->getError();
99            }
100
101            return in.readNode();
102        }
103
104        virtual WriteResult writeObject(const Object& object,const std::string& fileName, const osgDB::ReaderWriter::Options* options) const
105        {
106            const Node* node = dynamic_cast<const Node*>(&object);
107            if (node) return writeNode( *node, fileName, options );
108            const Image* image = dynamic_cast<const Image*>(&object);
109            if (image) return writeImage(*image, fileName, options);
110            return WriteResult::FILE_NOT_HANDLED;
111        }
112
113        virtual WriteResult writeImage(const Image& image,const std::string& fileName, const osgDB::ReaderWriter::Options* options) const
114        {
115            std::string ext = getFileExtension(fileName);
116            if (!acceptsExtension(ext)) return WriteResult::FILE_NOT_HANDLED;
117            // code for setting up the database path so that internally referenced file are searched for on relative paths.
118            osg::ref_ptr<Options> local_opt = options ? static_cast<Options*>(options->clone(osg::CopyOp::SHALLOW_COPY)) : new Options;
119            if(local_opt->getDatabasePathList().empty())
120                local_opt->setDatabasePath(osgDB::getFilePath(fileName));
121            osgDB::ofstream fout(fileName.c_str(), std::ios::out | std::ios::binary);
122            if (!fout) return WriteResult::ERROR_IN_WRITING_FILE;
123            WriteResult result = writeImage(image, fout, local_opt.get());
124            fout.close();
125            return result;
126        }
127
128        virtual WriteResult writeNode(const Node& node,const std::string& fileName, const osgDB::ReaderWriter::Options* options) const
129        {
130            std::string ext = getFileExtension(fileName);
131            if (!acceptsExtension(ext)) return WriteResult::FILE_NOT_HANDLED;
132
133            // code for setting up the database path so that internally referenced file are searched for on relative paths.
134            osg::ref_ptr<Options> local_opt = options ? static_cast<Options*>(options->clone(osg::CopyOp::SHALLOW_COPY)) : new Options;
135            if(local_opt->getDatabasePathList().empty())
136                local_opt->setDatabasePath(osgDB::getFilePath(fileName));
137
138            osgDB::ofstream fout(fileName.c_str(), std::ios::out | std::ios::binary);
139            if (!fout) return WriteResult::ERROR_IN_WRITING_FILE;
140   
141            WriteResult result = writeNode(node, fout, local_opt.get());
142            fout.close();
143            return result;
144        }
145       
146        virtual WriteResult writeObject(const Object& object,std::ostream& fout, const osgDB::ReaderWriter::Options* options) const
147        {
148            const Node* node = dynamic_cast<const Node*>(&object);
149            if (node) return writeNode( *node, fout, options );
150            const Image* image = dynamic_cast<const Image*>(&object);
151            if (image) return writeImage(*image, fout, options);
152            return WriteResult::FILE_NOT_HANDLED;
153        }
154
155        virtual WriteResult writeImage(const Image& image,std::ostream& fout, const osgDB::ReaderWriter::Options* options) const
156        {
157            ive::DataOutputStream out(&fout, options);
158            out.writeImage(ive::IMAGE_INCLUDE_DATA, const_cast<osg::Image*>(&image));
159            if (fout.fail()) return WriteResult::ERROR_IN_WRITING_FILE;
160            if (out.getException())
161            {
162                osg::notify(osg::WARN)<<"Error writing IVE image: "<< out.getException()->getError() << std::endl;
163                return WriteResult::FILE_NOT_HANDLED;
164            }
165            return WriteResult::FILE_SAVED;
166        }
167
168        virtual WriteResult writeNode(const Node& node,std::ostream& fout, const osgDB::ReaderWriter::Options* options) const
169        {
170            ive::DataOutputStream out(&fout, options);
171
172            out.writeNode(const_cast<osg::Node*>(&node));
173
174            if ( fout.fail() ) return WriteResult::ERROR_IN_WRITING_FILE;
175            if (out.getException())
176            {
177                osg::notify(osg::WARN)<<"Error writing IVE image: "<< out.getException()->getError() << std::endl;
178                return WriteResult::FILE_NOT_HANDLED;
179            }
180
181            return WriteResult::FILE_SAVED;
182        }
183
184};
185
186// now register with Registry to instantiate the above
187// reader/writer.
188REGISTER_OSGPLUGIN(ive, ReaderWriterIVE)
Note: See TracBrowser for help on using the browser.