From Eric Sokolowski, "Added the ability to read and write images directly in the ive plugin,

through the osgDB::readImageFile and osgDB::writeImageFile functions.
This is useful for storing compressed textures on disk for rapid playback
for animations."
This commit is contained in:
Robert Osfield 2006-07-03 09:26:12 +00:00
parent e7d04408d3
commit c90e35038a

View File

@ -25,6 +25,20 @@ class IVEReaderWriter : public ReaderWriter
return readNode(file, options); return readNode(file, options);
} }
virtual ReadResult readImage(const std::string& file, const Options* options) const
{
std::string ext = osgDB::getLowerCaseFileExtension(file);
if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED;
std::string fileName = osgDB::findDataFile(file, options);
if (fileName.empty()) return ReadResult::FILE_NOT_FOUND;
// code for setting up the database path so that internally referenced files are searched for on relative paths.
osg::ref_ptr<Options> local_opt = options ? static_cast<Options*>(options->clone(osg::CopyOp::SHALLOW_COPY)) : new Options;
local_opt->setDatabasePath(osgDB::getFilePath(fileName));
std::ifstream istream(fileName.c_str(), std::ios::in | std::ios::binary);
return readImage(istream, local_opt.get());
}
virtual ReadResult readNode(const std::string& file, const Options* options) const virtual ReadResult readNode(const std::string& file, const Options* options) const
{ {
std::string ext = osgDB::getLowerCaseFileExtension(file); std::string ext = osgDB::getLowerCaseFileExtension(file);
@ -46,6 +60,20 @@ class IVEReaderWriter : public ReaderWriter
return readNode(fin, options); return readNode(fin, options);
} }
virtual ReadResult readImage(std::istream& fin, const Options* options) const
{
try{
ive::DataInputStream in(&fin);
in.setOptions(options);
return in.readImage(ive::IMAGE_INCLUDE_DATA);
}
catch(ive::Exception e)
{
osg::notify(osg::NOTICE)<<"Error reading image: "<<e.getError()<<std::endl;
return ReadResult::FILE_NOT_HANDLED;
}
}
virtual ReadResult readNode(std::istream& fin, const Options* options) const virtual ReadResult readNode(std::istream& fin, const Options* options) const
{ {
try{ try{
@ -68,9 +96,26 @@ class IVEReaderWriter : public ReaderWriter
{ {
const Node* node = dynamic_cast<const Node*>(&object); const Node* node = dynamic_cast<const Node*>(&object);
if (node) return writeNode( *node, fileName, options ); if (node) return writeNode( *node, fileName, options );
const Image* image = dynamic_cast<const Image*>(&object);
if (image) return writeImage(*image, fileName, options);
return WriteResult::FILE_NOT_HANDLED; return WriteResult::FILE_NOT_HANDLED;
} }
virtual WriteResult writeImage(const Image& image,const std::string& fileName, const osgDB::ReaderWriter::Options* options) const
{
std::string ext = getFileExtension(fileName);
if (!acceptsExtension(ext)) return WriteResult::FILE_NOT_HANDLED;
// code for setting up the database path so that internally referenced file are searched for on relative paths.
osg::ref_ptr<Options> local_opt = options ? static_cast<Options*>(options->clone(osg::CopyOp::SHALLOW_COPY)) : new Options;
if(local_opt->getDatabasePathList().empty())
local_opt->setDatabasePath(osgDB::getFilePath(fileName));
std::ofstream fout(fileName.c_str(), std::ios::out | std::ios::binary);
if (!fout) return WriteResult::ERROR_IN_WRITING_FILE;
WriteResult result = writeImage(image, fout, local_opt.get());
fout.close();
return result;
}
virtual WriteResult writeNode(const Node& node,const std::string& fileName, const osgDB::ReaderWriter::Options* options) const virtual WriteResult writeNode(const Node& node,const std::string& fileName, const osgDB::ReaderWriter::Options* options) const
{ {
std::string ext = getFileExtension(fileName); std::string ext = getFileExtension(fileName);
@ -93,6 +138,25 @@ class IVEReaderWriter : public ReaderWriter
{ {
const Node* node = dynamic_cast<const Node*>(&object); const Node* node = dynamic_cast<const Node*>(&object);
if (node) return writeNode( *node, fout, options ); if (node) return writeNode( *node, fout, options );
const Image* image = dynamic_cast<const Image*>(&object);
if (image) return writeImage(*image, fout, options);
return WriteResult::FILE_NOT_HANDLED;
}
virtual WriteResult writeImage(const Image& image,std::ostream& fout, const osgDB::ReaderWriter::Options* options) const
{
try
{
ive::DataOutputStream out(&fout);
out.setOptions(options);
out.writeImage(ive::IMAGE_INCLUDE_DATA, const_cast<osg::Image*>(&image));
if (fout.fail()) return WriteResult::ERROR_IN_WRITING_FILE;
return WriteResult::FILE_SAVED;
}
catch(ive::Exception e)
{
osg::notify(osg::WARN)<<"Error writing IVE image: "<< e.getError() << std::endl;
}
return WriteResult::FILE_NOT_HANDLED; return WriteResult::FILE_NOT_HANDLED;
} }