Permalink
Browse files

Added ImageProcessor interface class and plugin mechnanism for ImageP…

…rocessor implementations to osgDB::Registry.

Add NVidiaTextureTools based plugin that provides an ImageProcessor implementation within an nvtt plugin.
  • Loading branch information...
1 parent 9c77274 commit a14219204452624e68c7dbdeae685d47a3a65d60 @osg-training-guest osg-training-guest committed Jan 13, 2011
View
@@ -423,6 +423,7 @@ FIND_PACKAGE(Poppler-glib)
FIND_PACKAGE(RSVG)
FIND_PACKAGE(GtkGl)
FIND_PACKAGE(DirectInput)
+FIND_PACKAGE(NVTT)
# Include macro utilities here
INCLUDE(OsgMacroUtils)
@@ -0,0 +1,61 @@
+# Locate nvidia-texture-tools
+# This module defines
+# NVTT_LIBRARY
+# NVTT_FOUND, if false, do not try to link to nvtt
+# NVTT_INCLUDE_DIR, where to find the headers
+#
+
+
+FIND_PATH(NVTT_INCLUDE_DIR nvtt/nvtt.h
+ PATHS
+ /usr/local
+ /usr
+ $ENV{NVTT_DIR}
+ ${3rdPartyRoot}
+ PATH_SUFFIXES include
+)
+
+FIND_LIBRARY(NVTT_LIBRARY
+ NAMES nvtt
+ PATHS
+ /usr/local
+ /usr
+ $ENV{NVTT_DIR}
+ ${3rdPartyRoot}
+ PATH_SUFFIXES lib64 lib lib/shared lib/static lib64/static
+)
+
+FIND_LIBRARY(NVIMAGE_LIBRARY
+ NAMES nvimage
+ PATHS
+ /usr/local
+ /usr
+ $ENV{NVTT_DIR}
+ ${3rdPartyRoot}
+ PATH_SUFFIXES lib64 lib lib/shared lib/static lib64/static
+)
+
+FIND_LIBRARY(NVMATH_LIBRARY
+ NAMES nvmath
+ PATHS
+ /usr/local
+ /usr
+ $ENV{NVTT_DIR}
+ ${3rdPartyRoot}
+ PATH_SUFFIXES lib64 lib lib/shared lib/static lib64/static
+)
+
+FIND_LIBRARY(NVCORE_LIBRARY
+ NAMES nvcore
+ PATHS
+ /usr/local
+ /usr
+ $ENV{NVTT_DIR}
+ ${3rdPartyRoot}
+ PATH_SUFFIXES lib64 lib lib/shared lib/static lib64/static
+)
+
+SET(NVTT_FOUND "NO")
+IF(NVTT_LIBRARY AND NVTT_INCLUDE_DIR)
+ SET(NVTT_FOUND "YES")
+ENDIF(NVTT_LIBRARY AND NVTT_INCLUDE_DIR)
@@ -0,0 +1,54 @@
+/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 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.
+*/
+
+#ifndef OSGDB_IMAGEPROCESSOR
+#define OSGDB_IMAGEPROCESSOR 1
+
+#include <osg/Object>
+
+namespace osgDB {
+
+class ImageProcessor : public osg::Object
+{
+ public:
+
+ ImageProcessor():
+ osg::Object(true) {}
+
+ ImageProcessor(const ImageProcessor& rw,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY):
+ osg::Object(rw,copyop) {}
+
+ virtual ~ImageProcessor() {}
+
+ META_Object(osgDB,ImageProcessor);
+
+ enum CompressionMethod
+ {
+ USE_CPU, /// Use CPU for compression even when GPU compression is available
+ USE_GPU /// Use GPU for compression when available (i.e CUDA), otherwise fallback to CPU
+ };
+
+ enum CompressionQuality
+ {
+ FASTEST,
+ NORMAL,
+ PRODUCTION,
+ HIGHEST
+ };
+
+ virtual void compress(osg::Image& image, osg::Texture::InternalFormatMode compressedFormat, bool generateMipMap, bool resizeToPowerOfTwo, CompressionMethod method, CompressionQuality quality) {}
+ virtual void generateMipMap(osg::Image& image, bool resizeToPowerOfTwo, CompressionMethod method) {}
+};
+
+}
+#endif
View
@@ -27,6 +27,7 @@
#include <osgDB/ObjectWrapper>
#include <osgDB/FileCache>
#include <osgDB/SharedStateManager>
+#include <osgDB/ImageProcessor>
#include <vector>
#include <map>
@@ -59,7 +60,6 @@ struct type_wrapper: basic_type_wrapper {
}
};
-
/**
Registry is a singleton factory which stores
the reader/writers which are linked in
@@ -101,6 +101,9 @@ class OSGDB_EXPORT Registry : public osg::Referenced
void addReaderWriter(ReaderWriter* rw);
void removeReaderWriter(ReaderWriter* rw);
+ void addImageProcessor(ImageProcessor* ip);
+ void removeImageProcessor(ImageProcessor* ip);
+
/** create the platform specific library name associated with file.*/
std::string createLibraryNameForFile(const std::string& fileName);
@@ -142,6 +145,21 @@ class OSGDB_EXPORT Registry : public osg::Referenced
const ReaderWriterList& getReaderWriterList() const { return _rwList; }
+ typedef std::vector< osg::ref_ptr<ImageProcessor> > ImageProcessorList;
+
+ /** get a image processor if available.*/
+ ImageProcessor* getImageProcessor();
+
+ /** get a image processor which is associated specified extension.*/
+ ImageProcessor* getImageProcessorForExtension(const std::string& ext);
+
+ /** get list of all registered ImageProcessors.*/
+ ImageProcessorList& getImageProcessorList() { return _ipList; }
+
+ /** get const list of all registered ImageProcessors.*/
+ const ImageProcessorList& getImageProcessorList() const { return _ipList; }
+
+
typedef class osgDB::FindFileCallback FindFileCallback;
typedef class osgDB::ReadFileCallback ReadFileCallback;
typedef class osgDB::WriteFileCallback WriteFileCallback;
@@ -555,6 +573,7 @@ class OSGDB_EXPORT Registry : public osg::Referenced
OpenThreads::ReentrantMutex _pluginMutex;
ReaderWriterList _rwList;
+ ImageProcessorList _ipList;
DynamicLibraryList _dlList;
bool _openingLibrary;
@@ -624,6 +643,34 @@ class RegisterReaderWriterProxy
};
+/** Proxy class for automatic registration of reader/writers with the Registry.*/
+template<class T>
+class RegisterImageProcessorProxy
+{
+ public:
+ RegisterImageProcessorProxy()
+ {
+ if (Registry::instance())
+ {
+ _rw = new T;
+ Registry::instance()->addImageProcessor(_rw.get());
+ }
+ }
+
+ ~RegisterImageProcessorProxy()
+ {
+ if (Registry::instance())
+ {
+ Registry::instance()->removeImageProcessor(_rw.get());
+ }
+ }
+
+ T* get() { return _rw.get(); }
+
+ protected:
+ osg::ref_ptr<T> _rw;
+};
+
struct PluginFunctionProxy
{
PluginFunctionProxy(CPluginFunction function) { (function)(); }
@@ -657,6 +704,9 @@ struct PluginFunctionProxy
extern "C" void osgdb_##ext(void) {} \
static osgDB::RegisterReaderWriterProxy<classname> g_proxy_##classname;
+#define REGISTER_OSGIMAGEPROCESSOR(ext, classname) \
+ extern "C" void osgdb_##ext(void) {} \
+ static osgDB::RegisterImageProcessorProxy<classname> g_proxy_##classname;
}
View
@@ -550,6 +550,71 @@ void Registry::removeReaderWriter(ReaderWriter* rw)
}
+ImageProcessor* Registry::getImageProcessor()
+{
+ {
+ OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(_pluginMutex);
+ if (!_ipList.empty())
+ {
+ return _ipList.front().get();
+ }
+ }
+ return getImageProcessorForExtension("nvtt");
+}
+
+ImageProcessor* Registry::getImageProcessorForExtension(const std::string& ext)
+{
+ {
+ OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(_pluginMutex);
+ if (!_ipList.empty())
+ {
+ return _ipList.front().get();
+ }
+ }
+
+ std::string libraryName = createLibraryNameForExtension(ext);
+ OSG_NOTICE << "Now checking for plug-in "<<libraryName<< std::endl;
+ if (loadLibrary(libraryName)==LOADED)
+ {
+ OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(_pluginMutex);
+ if (!_ipList.empty())
+ {
+ OSG_NOTICE << "Loaded plug-in "<<libraryName<<" and located ImageProcessor"<< std::endl;
+ return _ipList.front().get();
+ }
+ }
+ return 0;
+}
+
+void Registry::addImageProcessor(ImageProcessor* ip)
+{
+ if (ip==0L) return;
+
+ OSG_NOTIFY(NOTICE) << "osg::Registry::addImageProcessor("<<ip->className()<<")"<< std::endl;
+
+ OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(_pluginMutex);
+
+ _ipList.push_back(ip);
+
+}
+
+
+void Registry::removeImageProcessor(ImageProcessor* ip)
+{
+ if (ip==0L) return;
+
+ OSG_NOTIFY(NOTICE) << "osg::Registry::removeImageProcessor();"<< std::endl;
+
+ OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(_pluginMutex);
+
+ ImageProcessorList::iterator ipitr = std::find(_ipList.begin(),_ipList.end(),ip);
+ if (ipitr!=_ipList.end())
+ {
+ _ipList.erase(ipitr);
+ }
+
+}
+
void Registry::addFileExtensionAlias(const std::string mapExt, const std::string toExt)
{
@@ -215,6 +215,9 @@ IF(QTKIT_FOUND)
ADD_SUBDIRECTORY(QTKit)
ENDIF()
+IF(NVTT_FOUND)
+ ADD_SUBDIRECTORY(nvtt)
+ENDIF()
IF(FREETYPE_FOUND)
@@ -0,0 +1,11 @@
+INCLUDE_DIRECTORIES( ${NVTT_INCLUDE_DIR} )
+
+
+SET(TARGET_SRC
+ NVTTImageProcessor.cpp
+)
+
+SET(TARGET_LIBRARIES_VARS NVTT_LIBRARY)
+
+#### end var setup ###
+SETUP_PLUGIN(nvtt)
Oops, something went wrong.

0 comments on commit a142192

Please sign in to comment.