root/OpenSceneGraph/trunk/include/osgDB/OutputStream @ 11018

Revision 11018, 7.2 kB (checked in by robert, 5 years ago)

From Wang Rui, "Changes:

1. Rewrite the reading/writing exception handlers to work like the ive
plugin exceptions.
2. Write a header writing/checking function in ReaderWriterOSG2.cpp,
which may help decide if the stream is ascii or binary. The
readInputIterator() function will return null pointer if the input
file is nither osgb nor osgt format, which indicates that the old .osg
format could be used here, in case we've merged the two plugins
together.
3. Add a new ForceReadingImage? option in the InputStream?, which will
allocate an empty image object with the filename if specifed external
image file is missed. It may be useful for format converting in some
cases.
4. Add new osgParticle wrappers, as well as some modification to the
osgParticle headers, for instance, change isEnabled() to getEnabled().
5. Some fixes to the osg serialization wrappers."

Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * OpenSceneGraph Public License for more details.
12*/
13// Written by Wang Rui, (C) 2010
14
15#ifndef OSGDB_OUTPUTSTREAM
16#define OSGDB_OUTPUTSTREAM
17
18#include <osg/Vec2>
19#include <osg/Vec3>
20#include <osg/Vec4>
21#include <osg/Quat>
22#include <osg/Matrix>
23#include <osg/Array>
24#include <osg/PrimitiveSet>
25#include <osgDB/ReaderWriter>
26#include <osgDB/StreamOperator>
27#include <iostream>
28#include <sstream>
29
30namespace osgDB
31{
32
33class OutputException : public osg::Referenced
34{
35public:
36    OutputException( const std::vector<std::string>& fields, const std::string& err ) : _error(err)
37    {
38        for ( unsigned int i=0; i<fields.size(); ++i )
39        {
40            _field += fields[i];
41            _field += " ";
42        }
43    }
44   
45    const std::string& getField() const { return _field; }
46    const std::string& getError() const { return _error; }
47   
48protected:
49    std::string _field;
50    std::string _error;
51};
52
53class OSGDB_EXPORT OutputStream
54{
55public:
56    typedef std::map<const osg::Array*, unsigned int> ArrayMap;
57    typedef std::map<const osg::Object*, unsigned int> ObjectMap;
58   
59    enum WriteType
60    {
61        WRITE_UNKNOWN = 0,
62        WRITE_SCENE,
63        WRITE_IMAGE
64    };
65   
66    enum WriteImageHint
67    {
68        WRITE_USE_IMAGE_HINT = 0,  /*!< Use image hint, write inline data or use external */
69        WRITE_USE_EXTERNAL,        /*!< Use external file on disk and write only the filename */
70        WRITE_INLINE_DATA,         /*!< Write Image::data() to stream */
71        WRITE_INLINE_FILE,         /*!< Write the image file itself to stream */
72        WRITE_EXTERNAL_FILE        /*!< Write Image::data() to disk and use it as external file */
73    };
74   
75    OutputStream( const osgDB::Options* options );
76    virtual ~OutputStream();
77   
78    bool isBinary() const { return _out->isBinary(); }
79    const std::string& getSchemaName() const { return _schemaName; }
80   
81    void setWriteImageHint( WriteImageHint hint ) { _writeImageHint = hint; }
82    WriteImageHint getWriteImageHint() const { return _writeImageHint; }
83   
84    // Serialization related functions
85    OutputStream& operator<<( bool b ) { _out->writeBool(b); return *this; }
86    OutputStream& operator<<( char c ) { _out->writeChar(c); return *this; }
87    OutputStream& operator<<( unsigned char c ) { _out->writeUChar(c); return *this; }
88    OutputStream& operator<<( short s ) { _out->writeShort(s); return *this; }
89    OutputStream& operator<<( unsigned short s ) { _out->writeUShort(s); return *this; }
90    OutputStream& operator<<( int i ) { _out->writeInt(i); return *this; }
91    OutputStream& operator<<( unsigned int i ) { _out->writeUInt(i); return *this; }
92    OutputStream& operator<<( long l ) { _out->writeLong(l); return *this; }
93    OutputStream& operator<<( unsigned long l ) { _out->writeULong(l); return *this; }
94    OutputStream& operator<<( float f ) { _out->writeFloat(f); return *this; }
95    OutputStream& operator<<( double d ) { _out->writeDouble(d); return *this; }
96    OutputStream& operator<<( const std::string& s ) { _out->writeString(s); return *this; }
97    OutputStream& operator<<( std::ostream& (*fn)(std::ostream&) ) { _out->writeStream(fn); return *this; }
98    OutputStream& operator<<( std::ios_base& (*fn)(std::ios_base&) ) { _out->writeBase(fn); return *this; }
99   
100    OutputStream& operator<<( const ObjectGLenum& value ) { _out->writeGLenum(value); return *this; }
101    OutputStream& operator<<( const ObjectProperty& prop ) { _out->writeProperty(prop); return *this; }
102    OutputStream& operator<<( const ObjectMark& mark ) { _out->writeMark(mark); return *this; }
103   
104    OutputStream& operator<<( const osg::Vec2b& v );
105    OutputStream& operator<<( const osg::Vec3b& v );
106    OutputStream& operator<<( const osg::Vec4b& v );
107    OutputStream& operator<<( const osg::Vec4ub& v );
108    OutputStream& operator<<( const osg::Vec2s& v );
109    OutputStream& operator<<( const osg::Vec3s& v );
110    OutputStream& operator<<( const osg::Vec4s& v );
111    OutputStream& operator<<( const osg::Vec2f& v );
112    OutputStream& operator<<( const osg::Vec3f& v );
113    OutputStream& operator<<( const osg::Vec4f& v );
114    OutputStream& operator<<( const osg::Vec2d& v );
115    OutputStream& operator<<( const osg::Vec3d& v );
116    OutputStream& operator<<( const osg::Vec4d& v );
117    OutputStream& operator<<( const osg::Quat& q );
118    OutputStream& operator<<( const osg::Plane& p );
119    OutputStream& operator<<( const osg::Matrixf& mat );
120    OutputStream& operator<<( const osg::Matrixd& mat );
121   
122    OutputStream& operator<<( const osg::Array* a ) { writeArray(a); return *this; }
123    OutputStream& operator<<( const osg::Image* img ) { writeImage(img); return *this; }
124    OutputStream& operator<<( const osg::PrimitiveSet* p ) { writePrimitiveSet(p); return *this; }
125    OutputStream& operator<<( const osg::Object* obj ) { writeObject(obj); return *this; }
126   
127    OutputStream& operator<<( const osg::ref_ptr<osg::Array>& ptr ) { writeArray(ptr.get()); return *this; }
128    OutputStream& operator<<( const osg::ref_ptr<osg::Image>& ptr ) { writeImage(ptr.get()); return *this; }
129    OutputStream& operator<<( const osg::ref_ptr<osg::PrimitiveSet>& ptr ) { writePrimitiveSet(ptr.get()); return *this; }
130   
131    template<typename T> OutputStream& operator<<( const osg::ref_ptr<T>& ptr )
132    { writeObject(ptr.get()); return *this; }
133   
134    // Convenient methods for writing
135    void writeWrappedString( const std::string& str );
136    void writeCharArray( const char* s, unsigned int size ) { _out->writeCharArray(s, size); }
137   
138    // Global writing functions
139    void writeArray( const osg::Array* a );
140    void writePrimitiveSet( const osg::PrimitiveSet* p );
141    void writeImage( const osg::Image* img );
142    void writeObject( const osg::Object* obj );
143   
144    void start( OutputIterator* outIterator, WriteType type );
145    void compress( std::ostream* ostream );
146   
147    // Schema handlers
148    void writeSchema( std::ostream& fout );
149   
150    // Exception handlers
151    inline void throwException( const std::string& msg );
152    const OutputException* getException() const { return _exception.get(); }
153   
154protected:
155    template<typename T>
156    void writeArrayImplementation( const T*, int writeSize, unsigned int numInRow=1 );
157   
158    unsigned int findOrCreateArrayID( const osg::Array* array );
159    unsigned int findOrCreateObjectID( const osg::Object* obj );
160   
161    ArrayMap _arrayMap;
162    ObjectMap _objectMap;
163   
164    WriteImageHint _writeImageHint;
165    std::vector<std::string> _fields;
166    std::string _schemaName;
167    std::string _compressorName;
168    std::stringstream _compressSource;
169    osg::ref_ptr<OutputIterator> _out;
170    osg::ref_ptr<OutputException> _exception;
171};
172
173void OutputStream::throwException( const std::string& msg )
174{
175    _exception = new OutputException(_fields, msg);
176}
177
178}
179
180#endif
Note: See TracBrowser for help on using the browser.