OutputStream 8.15 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 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.
*/
// Written by Wang Rui, (C) 2010

#ifndef OSGDB_OUTPUTSTREAM
#define OSGDB_OUTPUTSTREAM

#include <osg/Vec2>
#include <osg/Vec3>
#include <osg/Vec4>
#include <osg/Quat>
#include <osg/Matrix>
#include <osg/Array>
#include <osg/PrimitiveSet>
#include <osgDB/ReaderWriter>
#include <osgDB/StreamOperator>
#include <iostream>
#include <sstream>

namespace osgDB
{

class OutputException : public osg::Referenced
{
public:
    OutputException( const std::vector<std::string>& fields, const std::string& err ) : _error(err)
    {
        for ( unsigned int i=0; i<fields.size(); ++i )
        {
            _field += fields[i];
            _field += " ";
        }
    }
    
    const std::string& getField() const { return _field; }
    const std::string& getError() const { return _error; }
    
protected:
    std::string _field;
    std::string _error;
};

class OSGDB_EXPORT OutputStream
{
public:
    typedef std::map<const osg::Array*, unsigned int> ArrayMap;
    typedef std::map<const osg::Object*, unsigned int> ObjectMap;
    
    enum WriteType
    {
        WRITE_UNKNOWN = 0,
        WRITE_SCENE,
        WRITE_IMAGE,
        WRITE_OBJECT
    };
    
    enum WriteImageHint
    {
        WRITE_USE_IMAGE_HINT = 0,  /*!< Use image hint, write inline data or use external */
        WRITE_USE_EXTERNAL,        /*!< Use external file on disk and write only the filename */
        WRITE_INLINE_DATA,         /*!< Write Image::data() to stream */
        WRITE_INLINE_FILE,         /*!< Write the image file itself to stream */
        WRITE_EXTERNAL_FILE        /*!< Write Image::data() to disk and use it as external file */
    };
    
    OutputStream( const osgDB::Options* options );
    virtual ~OutputStream();
    
    bool isBinary() const { return _out->isBinary(); }
    const std::string& getSchemaName() const { return _schemaName; }
    const osgDB::Options* getOptions() const { return _options.get(); }
    
    void setWriteImageHint( WriteImageHint hint ) { _writeImageHint = hint; }
    WriteImageHint getWriteImageHint() const { return _writeImageHint; }
    
    // Serialization related functions
    OutputStream& operator<<( bool b ) { _out->writeBool(b); return *this; }
    OutputStream& operator<<( char c ) { _out->writeChar(c); return *this; }
    OutputStream& operator<<( unsigned char c ) { _out->writeUChar(c); return *this; }
    OutputStream& operator<<( short s ) { _out->writeShort(s); return *this; }
    OutputStream& operator<<( unsigned short s ) { _out->writeUShort(s); return *this; }
    OutputStream& operator<<( int i ) { _out->writeInt(i); return *this; }
    OutputStream& operator<<( unsigned int i ) { _out->writeUInt(i); return *this; }
    OutputStream& operator<<( long l ) { _out->writeLong(l); return *this; }
    OutputStream& operator<<( unsigned long l ) { _out->writeULong(l); return *this; }
    OutputStream& operator<<( float f ) { _out->writeFloat(f); return *this; }
    OutputStream& operator<<( double d ) { _out->writeDouble(d); return *this; }
    OutputStream& operator<<( const std::string& s ) { _out->writeString(s); return *this; }
    OutputStream& operator<<( const char* s ) { _out->writeString(s); return *this; }
    OutputStream& operator<<( std::ostream& (*fn)(std::ostream&) ) { _out->writeStream(fn); return *this; }
    OutputStream& operator<<( std::ios_base& (*fn)(std::ios_base&) ) { _out->writeBase(fn); return *this; }
    
    OutputStream& operator<<( const ObjectGLenum& value ) { _out->writeGLenum(value); return *this; }
    OutputStream& operator<<( const ObjectProperty& prop ) { _out->writeProperty(prop); return *this; }
    OutputStream& operator<<( const ObjectMark& mark ) { _out->writeMark(mark); return *this; }
    
    OutputStream& operator<<( const osg::Vec2b& v );
    OutputStream& operator<<( const osg::Vec3b& v );
    OutputStream& operator<<( const osg::Vec4b& v );
    OutputStream& operator<<( const osg::Vec4ub& v );
    OutputStream& operator<<( const osg::Vec2s& v );
    OutputStream& operator<<( const osg::Vec3s& v );
    OutputStream& operator<<( const osg::Vec4s& v );
    OutputStream& operator<<( const osg::Vec2f& v );
    OutputStream& operator<<( const osg::Vec3f& v );
    OutputStream& operator<<( const osg::Vec4f& v );
    OutputStream& operator<<( const osg::Vec2d& v );
    OutputStream& operator<<( const osg::Vec3d& v );
    OutputStream& operator<<( const osg::Vec4d& v );
    OutputStream& operator<<( const osg::Quat& q );
    OutputStream& operator<<( const osg::Plane& p );
    OutputStream& operator<<( const osg::Matrixf& mat );
    OutputStream& operator<<( const osg::Matrixd& mat );
    
    OutputStream& operator<<( const osg::Array* a ) { writeArray(a); return *this; }
    OutputStream& operator<<( const osg::Image* img ) { writeImage(img); return *this; }
    OutputStream& operator<<( const osg::PrimitiveSet* p ) { writePrimitiveSet(p); return *this; }
    OutputStream& operator<<( const osg::Object* obj ) { writeObject(obj); return *this; }
    
    OutputStream& operator<<( const osg::ref_ptr<osg::Array>& ptr ) { writeArray(ptr.get()); return *this; }
    OutputStream& operator<<( const osg::ref_ptr<osg::Image>& ptr ) { writeImage(ptr.get()); return *this; }
    OutputStream& operator<<( const osg::ref_ptr<osg::PrimitiveSet>& ptr ) { writePrimitiveSet(ptr.get()); return *this; }
    
    template<typename T> OutputStream& operator<<( const osg::ref_ptr<T>& ptr )
    { writeObject(ptr.get()); return *this; }
    
    // Convenient methods for writing
    void writeWrappedString( const std::string& str ) { _out->writeWrappedString(str); }
    void writeCharArray( const char* s, unsigned int size ) { _out->writeCharArray(s, size); }

    // method for converting all data structure sizes to unsigned int to ensure architecture portability.
    template<typename T>
    void writeSize(T size) { *this<<static_cast<unsigned int>(size); }

    // Global writing functions
    void writeArray( const osg::Array* a );
    void writePrimitiveSet( const osg::PrimitiveSet* p );
    void writeImage( const osg::Image* img );
    void writeObject( const osg::Object* obj );
    void writeObjectFields( const osg::Object* obj );
    
    /// set an output iterator, used directly when not using OutputStream with a traditional file releated stream.
    void setOutputIterator( OutputIterator* oi ) { _out = oi; }

    /// start writing to OutputStream treating it as a traditional file releated stream, handles headers and versioning
    void start( OutputIterator* outIterator, WriteType type );

    void compress( std::ostream* ostream );
    
    // Schema handlers
    void writeSchema( std::ostream& fout );
    
    // Exception handlers
    inline void throwException( const std::string& msg );
    const OutputException* getException() const { return _exception.get(); }
    
protected:
    template<typename T>
    void writeArrayImplementation( const T*, int write_size, unsigned int numInRow=1 );
    
    unsigned int findOrCreateArrayID( const osg::Array* array, bool& newID );
    unsigned int findOrCreateObjectID( const osg::Object* obj, bool& newID );
    
    ArrayMap _arrayMap;
    ObjectMap _objectMap;
    
    WriteImageHint _writeImageHint;
    bool _useSchemaData;
    std::map<std::string, std::string> _inbuiltSchemaMap;
    std::vector<std::string> _fields;
    std::string _schemaName;
    std::string _compressorName;
    std::stringstream _compressSource;
    osg::ref_ptr<OutputIterator> _out;
    osg::ref_ptr<OutputException> _exception;
    osg::ref_ptr<const osgDB::Options> _options;
};

void OutputStream::throwException( const std::string& msg )
{
    _exception = new OutputException(_fields, msg);
}

}

#endif