Difference between revisions of "Plugin IO mechanisms"

From KitwarePublic
Jump to navigationJump to search
Line 7: Line 7:
  MyImageIOFactory::MyImageIOFactory()
  MyImageIOFactory::MyImageIOFactory()
  {
  {
   this->RegisterOverride("itkImageIOBase",
   this->RegisterOverride("itkImageIOBase",
                         "itkMyImageIO",
                         "itkMyImageIO",
Line 29: Line 28:
** This is useful if you have existing ITK applications that you do not wish to recompile but you would like them to be able to use your new file format.
** This is useful if you have existing ITK applications that you do not wish to recompile but you would like them to be able to use your new file format.


=== Plugin mechanism ===
== Plugin mechanism ==


ITK's object factory mechanism supports dynamically loading factories from shared libraries. This mechanism can be used to dynamically load factories for new image file formats. The object factory will look at the ITK_AUTOLOAD_PATH environment variable to locate dynamically loadable factories (plugins).
ITK's object factory mechanism supports dynamically loading factories from shared libraries. This mechanism can be used to dynamically load factories for new image file formats. The object factory will look at the ITK_AUTOLOAD_PATH environment variable to locate dynamically loadable factories (plugins).
Line 41: Line 40:
If the method itkLoad() is found in the shared library, it is called.  itkLoad() must return a pointer to an ObjectFactoryBase.  The object factory mechanism then calls ObjectFactoryBase::RegisteryFactory(), passing in the pointer returned by itkLoad().  
If the method itkLoad() is found in the shared library, it is called.  itkLoad() must return a pointer to an ObjectFactoryBase.  The object factory mechanism then calls ObjectFactoryBase::RegisteryFactory(), passing in the pointer returned by itkLoad().  


=== Example ===
== Example ==


To add MyImageIOFactory as a dynamically loaded factory
To add MyImageIOFactory as a dynamically loaded factory

Revision as of 07:56, 30 April 2005

Plugin IO mechanisms

ITK provides a very flexible ImageIO mechanism supporting factories. This allows for a single reader class and a single writer class that can input/output a variety of file formats. When ImageFileReader begins to read a file, it queries the ObjectFactory system for any factory that can supply an ImageIOBase. This subset of factories is queried in turn to locate a factory that can understand the format of the requested file.

New image file formats can be added to ITK by deriving a class from ImageIOBase that will support reading/writing the new file format. The file format can be added to the factory mechanism by creating a subclass of ObjectFactoryBase for this new ImageIO class. This subclass of ObjectFactoryBase will create an override in the factory mechanism indicating it can create an ImageIOBase. This is done in the constructor of the subclass of ObjectFactoryBase for the new image format:

MyImageIOFactory::MyImageIOFactory()
{
 this->RegisterOverride("itkImageIOBase",
                        "itkMyImageIO",
                        "My Image IO",
                        1,
                        CreateObjectFunction<MyImageIO>::New()); 
}

Registering a factory

For the factory mechanism to use factory for the new image format, the new factory must be "registered" with the factory mechanism. A stock set of such factories are registered automatically by ImageIOFactory. To register additional factories,

  • Add the new format format to the list of formats registered by default in ImageIOFactory::RegisterBuiltInFactories().
    • This is should only be done if the new format is to be widely adopted by the entire ITK community.
  • Register the factory in your application code that uses that factory
ObjectFactoryBase::RegisterFactory( MyImageIOFactory::New() ); 
    • This is reasonable if you only need to use the file format in your own applications.
  • Use the factory plugin mechanism to dynamically register the factory.
    • This is useful if you have existing ITK applications that you do not wish to recompile but you would like them to be able to use your new file format.

Plugin mechanism

ITK's object factory mechanism supports dynamically loading factories from shared libraries. This mechanism can be used to dynamically load factories for new image file formats. The object factory will look at the ITK_AUTOLOAD_PATH environment variable to locate dynamically loadable factories (plugins).

export ITK_AUTOLOAD_PATH=/home/me/myplugins

If the ITK_AUTOLOAD_PATH is set, each shared library in each directory in that path is examined for a method

ObjectFactorBase* itkLoad()

If the method itkLoad() is found in the shared library, it is called. itkLoad() must return a pointer to an ObjectFactoryBase. The object factory mechanism then calls ObjectFactoryBase::RegisteryFactory(), passing in the pointer returned by itkLoad().

Example

To add MyImageIOFactory as a dynamically loaded factory