From e35b0796d6ee5c8191cecf3c8ef3ea31df3ee139 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Mon, 15 Sep 2014 17:53:44 +0000 Subject: [PATCH] Added .tf & .tf-255 plugin for reading ascii 1D transfer functon files in support for volume rendering. git-svn-id: http://svn.openscenegraph.org/osg/OpenSceneGraph/trunk@14448 16af8721-9629-0410-8352-f15c8da7e697 --- src/osgDB/Registry.cpp | 3 + src/osgPlugins/CMakeLists.txt | 1 + src/osgPlugins/tf/CMakeLists.txt | 3 + src/osgPlugins/tf/ReaderWriterTF.cpp | 137 +++++++++++++++++++++++++++ 4 files changed, 144 insertions(+) create mode 100644 src/osgPlugins/tf/CMakeLists.txt create mode 100644 src/osgPlugins/tf/ReaderWriterTF.cpp diff --git a/src/osgDB/Registry.cpp b/src/osgDB/Registry.cpp index e759ff4b2..6e9858c09 100644 --- a/src/osgDB/Registry.cpp +++ b/src/osgDB/Registry.cpp @@ -424,6 +424,9 @@ Registry::Registry() // addFileExtensionAlias("pfa", "freetype"); // type2 ascii + // TransferFunction head + addFileExtensionAlias("tf-255", "tf"); + // portable bitmap, greyscale and colour/pixmap image formats addFileExtensionAlias("pbm", "pnm"); addFileExtensionAlias("pgm", "pnm"); diff --git a/src/osgPlugins/CMakeLists.txt b/src/osgPlugins/CMakeLists.txt index 1a6c4a381..66c3f055c 100644 --- a/src/osgPlugins/CMakeLists.txt +++ b/src/osgPlugins/CMakeLists.txt @@ -257,6 +257,7 @@ ADD_SUBDIRECTORY(pvr) ADD_SUBDIRECTORY(osc) ADD_SUBDIRECTORY(trk) +ADD_SUBDIRECTORY(tf) #################################################### diff --git a/src/osgPlugins/tf/CMakeLists.txt b/src/osgPlugins/tf/CMakeLists.txt new file mode 100644 index 000000000..f968c8b84 --- /dev/null +++ b/src/osgPlugins/tf/CMakeLists.txt @@ -0,0 +1,3 @@ +SET(TARGET_SRC ReaderWriterTF.cpp ) +#### end var setup ### +SETUP_PLUGIN(tf) diff --git a/src/osgPlugins/tf/ReaderWriterTF.cpp b/src/osgPlugins/tf/ReaderWriterTF.cpp new file mode 100644 index 000000000..46d53c62c --- /dev/null +++ b/src/osgPlugins/tf/ReaderWriterTF.cpp @@ -0,0 +1,137 @@ +// Released under the OSGPL license, as part of the OpenSceneGraph distribution. +// +// Implementation of a native TransferFunction ascii file format. +// +#include + +#include + +#include +#include +#include + +#include +#include +#include + +class ReaderWriterTF : public osgDB::ReaderWriter +{ + public: + + ReaderWriterTF() + { + supportsExtension("tf","TransferFunction format"); + supportsExtension("tf-255","TransferFunction format"); + } + + virtual const char* className() const { return "TransferFunction Reader/Writer"; } + + ReadResult readTransferFunction(std::istream& fin, float colorScale) const + { + osg::TransferFunction1D::ColorMap colorMap; + while(fin) + { + float value, red, green, blue, alpha; + fin >> value >> red >> green >> blue >> alpha; + if (fin) + { + std::cout<<"value = "<second; + if (c.r()>maxValue) maxValue = c.r(); + if (c.g()>maxValue) maxValue = c.g(); + if (c.b()>maxValue) maxValue = c.b(); + if (c.a()>maxValue) maxValue = c.a(); + } + + if (maxValue>=2.0f) + { + colorScale = 1.0f/255.0f; + } + } + + if (colorScale!=0.0) + { + OSG_NOTICE<<"Rescaling ColorMap by "<second; + c.r() *= colorScale; + c.g() *= colorScale; + c.b() *= colorScale; + c.a() *= colorScale; + } + } + + osg::TransferFunction1D* tf = new osg::TransferFunction1D; + tf->assign(colorMap); + + return tf; + } + + bool readColorScale(const osgDB::ReaderWriter::Options* options, float& colorScale) const + { + if (options && options->getOptionString().find("tf-255")!=std::string::npos) + { + colorScale = 1.0/255.0f; + return true; + } + else + { + return false; + } + + } + + virtual ReadResult readObject(std::istream& fin,const osgDB::ReaderWriter::Options* options =NULL) const + { + float colorScale = 0.0f; // default auto-detect scale + + readColorScale(options, colorScale); + + return readTransferFunction(fin, colorScale); + } + + virtual ReadResult readObject(const std::string& file, const osgDB::ReaderWriter::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; + + float colorScale = 0.0f; // default auto-detect scale + if (ext=="tf-255") colorScale = 1.0f/255.0f; + + readColorScale(options, colorScale); + + osgDB::ifstream fin(fileName.c_str(), std::ios::in | std::ios::binary); + + if (fin) return readTransferFunction( fin, colorScale); + else return ReadResult::ERROR_IN_READING_FILE; + } + +}; + +// now register with Registry to instantiate the above +// reader/writer. +REGISTER_OSGPLUGIN(tf, ReaderWriterTF)