| 26 | | unsigned int headerLow = 0, headerHigh = 0; |
| 27 | | fin->read( (char*)&headerLow, INT_SIZE ); |
| 28 | | fin->read( (char*)&headerHigh, INT_SIZE ); |
| 29 | | if ( headerLow!=OSG_HEADER_LOW || headerHigh!=OSG_HEADER_HIGH ) |
| 30 | | { |
| 31 | | fin->seekg( 0, std::ios::beg ); |
| 32 | | return false; |
| 33 | | } |
| 34 | | return true; |
| | 29 | bool extensionIsAscii = false; |
| | 30 | if ( options && options->getOptionString().find("Ascii")!=std::string::npos ) |
| | 31 | extensionIsAscii = true; |
| | 32 | |
| | 33 | if ( !extensionIsAscii ) |
| | 34 | { |
| | 35 | unsigned int headerLow = 0, headerHigh = 0; |
| | 36 | fin.read( (char*)&headerLow, INT_SIZE ); |
| | 37 | fin.read( (char*)&headerHigh, INT_SIZE ); |
| | 38 | if ( headerLow==OSG_HEADER_LOW && headerHigh==OSG_HEADER_HIGH ) |
| | 39 | { |
| | 40 | return new BinaryInputIterator(&fin); |
| | 41 | } |
| | 42 | fin.seekg( 0, std::ios::beg ); |
| | 43 | } |
| | 44 | |
| | 45 | std::string header; fin >> header; |
| | 46 | if ( header=="#Ascii" ) |
| | 47 | { |
| | 48 | return new AsciiInputIterator(&fin); |
| | 49 | } |
| | 50 | return NULL; |
| | 51 | } |
| | 52 | |
| | 53 | OutputIterator* writeInputIterator( std::ostream& fout, const Options* options ) |
| | 54 | { |
| | 55 | if ( options && options->getOptionString().find("Ascii")!=std::string::npos ) |
| | 56 | { |
| | 57 | fout << std::string("#Ascii") << ' '; |
| | 58 | return new AsciiOutputIterator(&fout); |
| | 59 | } |
| | 60 | else |
| | 61 | { |
| | 62 | unsigned int low = OSG_HEADER_LOW, high = OSG_HEADER_HIGH; |
| | 63 | fout.write( (char*)&low, INT_SIZE ); |
| | 64 | fout.write( (char*)&high, INT_SIZE ); |
| | 65 | return new BinaryOutputIterator(&fout); |
| | 66 | } |
| 92 | | try |
| 93 | | { |
| 94 | | InputStream is( options ); |
| 95 | | |
| 96 | | InputIterator* ii = NULL; |
| 97 | | if ( !checkBinary(&fin) ) |
| 98 | | ii = new AsciiInputIterator(&fin); |
| 99 | | else |
| 100 | | ii = new BinaryInputIterator(&fin); |
| 101 | | |
| 102 | | if ( is.start(ii)!=InputStream::READ_IMAGE ) |
| 103 | | return ReadResult::FILE_NOT_HANDLED; |
| 104 | | is.decompress(); |
| 105 | | return is.readImage(); |
| 106 | | } |
| 107 | | catch ( InputException e ) |
| 108 | | { |
| 109 | | return e.getError() + " At " + e.getField(); |
| 110 | | } |
| | 126 | osg::ref_ptr<InputIterator> ii = readInputIterator(fin, options); |
| | 127 | if ( !ii ) return ReadResult::FILE_NOT_HANDLED; |
| | 128 | |
| | 129 | InputStream is( options ); |
| | 130 | if ( is.start(ii.get())!=InputStream::READ_IMAGE ) |
| | 131 | { |
| | 132 | CATCH_EXCEPTION(is); |
| | 133 | return ReadResult::FILE_NOT_HANDLED; |
| | 134 | } |
| | 135 | is.decompress(); CATCH_EXCEPTION(is); |
| | 136 | osg::Image* image = is.readImage(); CATCH_EXCEPTION(is); |
| | 137 | return image; |
| 130 | | try |
| 131 | | { |
| 132 | | InputStream is( options ); |
| 133 | | |
| 134 | | InputIterator* ii = NULL; |
| 135 | | if ( !checkBinary(&fin) ) |
| 136 | | ii = new AsciiInputIterator(&fin); |
| 137 | | else |
| 138 | | ii = new BinaryInputIterator(&fin); |
| 139 | | |
| 140 | | if ( is.start(ii)!=InputStream::READ_SCENE ) |
| 141 | | return ReadResult::FILE_NOT_HANDLED; |
| 142 | | is.decompress(); |
| 143 | | return dynamic_cast<osg::Node*>( is.readObject() ); |
| 144 | | } |
| 145 | | catch ( InputException e ) |
| 146 | | { |
| 147 | | return e.getError() + " At " + e.getField(); |
| 148 | | } |
| | 158 | osg::ref_ptr<InputIterator> ii = readInputIterator(fin, options); |
| | 159 | if ( !ii ) return ReadResult::FILE_NOT_HANDLED; |
| | 160 | |
| | 161 | InputStream is( options ); |
| | 162 | if ( is.start(ii.get())!=InputStream::READ_SCENE ) |
| | 163 | { |
| | 164 | CATCH_EXCEPTION(is); |
| | 165 | return ReadResult::FILE_NOT_HANDLED; |
| | 166 | } |
| | 167 | |
| | 168 | is.decompress(); CATCH_EXCEPTION(is); |
| | 169 | osg::Node* node = dynamic_cast<osg::Node*>(is.readObject()); CATCH_EXCEPTION(is); |
| | 170 | return node; |
| 192 | | try |
| 193 | | { |
| 194 | | OutputStream os( options ); |
| 195 | | |
| 196 | | osgDB::OutputIterator* oi = NULL; |
| 197 | | if ( options && options->getOptionString().find("Ascii")!=std::string::npos ) |
| 198 | | oi = new AsciiOutputIterator(&fout); |
| 199 | | else |
| 200 | | oi = new BinaryOutputIterator(&fout); |
| 201 | | |
| 202 | | os.start( oi, OutputStream::WRITE_IMAGE ); |
| 203 | | os.writeImage( &image ); |
| 204 | | os.compress( &fout ); |
| 205 | | |
| 206 | | if ( fout.fail() ) return WriteResult::ERROR_IN_WRITING_FILE; |
| 207 | | return WriteResult::FILE_SAVED; |
| 208 | | } |
| 209 | | catch ( OutputException e ) |
| 210 | | { |
| 211 | | osg::notify(osg::WARN) << "ReaderWriterOSG2::writeImage(): " << e.getError() |
| 212 | | << " At " << e.getField() << std::endl; |
| 213 | | } |
| 214 | | return WriteResult::FILE_NOT_HANDLED; |
| | 214 | osg::ref_ptr<OutputIterator> oi = writeInputIterator(fout, options); |
| | 215 | |
| | 216 | OutputStream os( options ); |
| | 217 | os.start( oi.get(), OutputStream::WRITE_IMAGE ); CATCH_EXCEPTION(os); |
| | 218 | os.writeImage( &image ); CATCH_EXCEPTION(os); |
| | 219 | os.compress( &fout ); CATCH_EXCEPTION(os); |
| | 220 | |
| | 221 | if ( !os.getSchemaName().empty() ) |
| | 222 | { |
| | 223 | osgDB::ofstream schemaStream( os.getSchemaName().c_str(), std::ios::out ); |
| | 224 | if ( !schemaStream.fail() ) os.writeSchema( schemaStream ); |
| | 225 | schemaStream.close(); |
| | 226 | } |
| | 227 | |
| | 228 | if ( fout.fail() ) return WriteResult::ERROR_IN_WRITING_FILE; |
| | 229 | return WriteResult::FILE_SAVED; |
| 238 | | try |
| 239 | | { |
| 240 | | OutputStream os( options ); |
| 241 | | |
| 242 | | osgDB::OutputIterator* oi = NULL; |
| 243 | | if ( options && options->getOptionString().find("Ascii")!=std::string::npos ) |
| 244 | | oi = new AsciiOutputIterator(&fout); |
| 245 | | else |
| 246 | | oi = new BinaryOutputIterator(&fout); |
| 247 | | |
| 248 | | os.start( oi, OutputStream::WRITE_SCENE ); |
| 249 | | os.writeObject( &node ); |
| 250 | | os.compress( &fout ); |
| 251 | | |
| 252 | | if ( fout.fail() ) return WriteResult::ERROR_IN_WRITING_FILE; |
| 253 | | return WriteResult::FILE_SAVED; |
| 254 | | } |
| 255 | | catch ( OutputException e ) |
| 256 | | { |
| 257 | | osg::notify(osg::WARN) << "ReaderWriterOSG2::writeNode(): " << e.getError() |
| 258 | | << " At " << e.getField() << std::endl; |
| 259 | | } |
| 260 | | return WriteResult::FILE_NOT_HANDLED; |
| | 253 | osg::ref_ptr<OutputIterator> oi = writeInputIterator(fout, options); |
| | 254 | |
| | 255 | OutputStream os( options ); |
| | 256 | os.start( oi.get(), OutputStream::WRITE_SCENE ); CATCH_EXCEPTION(os); |
| | 257 | os.writeObject( &node ); CATCH_EXCEPTION(os); |
| | 258 | os.compress( &fout ); CATCH_EXCEPTION(os); |
| | 259 | |
| | 260 | if ( !os.getSchemaName().empty() ) |
| | 261 | { |
| | 262 | osgDB::ofstream schemaStream( os.getSchemaName().c_str(), std::ios::out ); |
| | 263 | if ( !schemaStream.fail() ) os.writeSchema( schemaStream ); |
| | 264 | schemaStream.close(); |
| | 265 | } |
| | 266 | |
| | 267 | if ( fout.fail() ) return WriteResult::ERROR_IN_WRITING_FILE; |
| | 268 | return WriteResult::FILE_SAVED; |