From a0eb4c72a15f0a79f3f33d50fe6e497a8285b3bc Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Thu, 11 Nov 2004 16:05:55 +0000 Subject: [PATCH] Added write to ostream to ReaderWriterDDS --- src/osgPlugins/dds/ReaderWriterDDS.cpp | 56 +++++++++++++++++++------- 1 file changed, 42 insertions(+), 14 deletions(-) diff --git a/src/osgPlugins/dds/ReaderWriterDDS.cpp b/src/osgPlugins/dds/ReaderWriterDDS.cpp index f14a1f910..a7545c590 100644 --- a/src/osgPlugins/dds/ReaderWriterDDS.cpp +++ b/src/osgPlugins/dds/ReaderWriterDDS.cpp @@ -457,13 +457,9 @@ printf("CORRECTO**> gtsibim:%d grsib:%d mi_size:%d lPitch%d\n", -bool WriteDDSFile(const osg::Image *img, const char *filename) +bool WriteDDSFile(const osg::Image *img, std::ostream& fout) { - FILE *fp = fopen(filename, "wb"); - if (!fp) - return false; - // Initialize ddsd structure and its members DDSURFACEDESC2 ddsd; DDPIXELFORMAT ddpf; @@ -636,7 +632,7 @@ bool WriteDDSFile(const osg::Image *img, const char *filename) memcpy(header, ddsName, 4); memcpy(header+4, &ddsd, sizeof(ddsd)); - fwrite(header, sizeof(ddsd)+4, 1, fp); /* write file header */ + fout.write(reinterpret_cast(header), sizeof(ddsd)+4); /* write file header */ int isize = img->getTotalSizeInBytesIncludingMipmaps(); unsigned char *buffer = new unsigned char[isize]; @@ -685,15 +681,13 @@ bool WriteDDSFile(const osg::Image *img, const char *filename) } // Check for correct saving - if (fwrite(buffer, isize , 1, fp) != 1) + fout.write(reinterpret_cast(buffer), isize); + + if (fout.fail()) { - fclose(fp); return false; } - // close file pointer - fclose(fp); - // If we get that far the file was saved properly // return true; } @@ -712,6 +706,16 @@ public: return osgDB::equalCaseInsensitive(extension,"dds"); } + virtual ReadResult readObject(const std::string& file, const osgDB::ReaderWriter::Options* options) + { + return readImage(file,options); + } + + virtual ReadResult readObject(std::istream& fin, const Options* options) + { + return readImage(fin,options); + } + virtual ReadResult readImage(const std::string& file, const osgDB::ReaderWriter::Options* options) { std::string ext = osgDB::getLowerCaseFileExtension(file); @@ -740,18 +744,42 @@ public: return osgImage; } - virtual WriteResult writeImage(const osg::Image &img,const std::string& file, const osgDB::ReaderWriter::Options*) + virtual WriteResult writeObject(const osg::Object& object,const std::string& file, const osgDB::ReaderWriter::Options* options) + { + const osg::Image* image = dynamic_cast(&object); + if (!image) return WriteResult::FILE_NOT_HANDLED; + + return writeImage(*image,file,options); + } + + virtual WriteResult writeObject(const osg::Object& object,std::ostream& fout,const Options* options) + { + const osg::Image* image = dynamic_cast(&object); + if (!image) return WriteResult::FILE_NOT_HANDLED; + + return writeImage(*image,fout,options); + } + + + virtual WriteResult writeImage(const osg::Image &image,const std::string& file, const osgDB::ReaderWriter::Options* options) { std::string ext = osgDB::getFileExtension(file); if (!acceptsExtension(ext)) return WriteResult::FILE_NOT_HANDLED; - bool success = WriteDDSFile(&img, file.c_str()); + std::ofstream fout(file.c_str(), std::ios::out | std::ios::binary); + if(!fout) return WriteResult::ERROR_IN_WRITING_FILE; + + return writeImage(image,fout,options); + } + + virtual WriteResult writeImage(const osg::Image& image,std::ostream& fout,const Options*) + { + bool success = WriteDDSFile(&image, fout); if(success) return WriteResult::FILE_SAVED; else return WriteResult::ERROR_IN_WRITING_FILE; - } };