Explanation :
It would be nice to let the user override the default built-in Plugin with his own.
And that's currently not possible :
when you try to register a local plugin for "DDS" extension files, the function FreeImage_RegisterLocalPlugin( xxx ) will just fail, since internally it already founds a plugin for the "DDS" format.
Proposed solution :
From an interface POV,
Anonymous
You first need to disable the DDS plugin before replacing it.
See e.g. the following sample :
void RegisterMyPlugin() {
FREE_IMAGE_FORMAT fif = FreeImage_RegisterLocalPlugin(InitMyPlugin);
// disable original My plugin
FreeImage_SetPluginEnabled(FIF_MY, FALSE);
// enable new My plugin
FreeImage_SetPluginEnabled(fif, TRUE);
}
or for other multi-format plugins (such as PNM plugin) :
void RegisterPNM() {
FREE_IMAGE_FORMAT fif = FreeImage_RegisterLocalPlugin(InitPNM);
// disable original pnm plugin
FreeImage_SetPluginEnabled(FIF_PBMRAW, FALSE);
FreeImage_SetPluginEnabled(FIF_PBM, FALSE);
FreeImage_SetPluginEnabled(FIF_PGMRAW, FALSE);
FreeImage_SetPluginEnabled(FIF_PGM, FALSE);
FreeImage_SetPluginEnabled(FIF_PPMRAW, FALSE);
FreeImage_SetPluginEnabled(FIF_PPM, FALSE);
// enable new pnm plugin
FreeImage_SetPluginEnabled(fif, TRUE);
}
Unfortunately, no, your function RegisterMyPlugin() is not working. Here is why :
If you call :
like you did your example, without specifying a format (const char*), then internally it will fallback to the function Format( xxx ) registered with your local plugin via the
plugin->format_proc = Format;
However, since you intend to replace a built-in plugin, you will have a Format( xxx ) function returning a known Format like this "DDS".
But unfortunately, since there is already a plugin associated to this Format (the builtin plugin that I intend to override), it will end here and never register my local plugin.
See :
{{{
if (the_format != NULL) {
if (FindNodeFromFormat(the_format) == NULL) {
node->m_id = (int)m_plugin_map.size();
node->m_instance = instance;
node->m_plugin = plugin;
node->m_format = format;
node->m_description = description;
node->m_extension = extension;
node->m_regexpr = regexpr;
node->m_enabled = TRUE;
}}}
And by the way, local plugin registered is always enabled, so no need to call
FreeImage_SetPluginEnabled(fif, TRUE);
For the record, my current work around is to call
Yes, that's dirty... >.< And it only works because I am working with image from memory.
Imagine I would want to load an image from its filepath, then some other functions of FreeImage would work properly either, like FreeImage_GetFIFFromFilename( xxx ).
Ooops, I forgot a "NOT" in the last sentence :
Imagine I would want to load an image from its filepath, then some other
functions of FreeImage would NOT work properly either, like
FreeImage_GetFIFFromFilename( xxx ).
When you disable a plugin, you must NOT use its FIF_FORMAT anymore.
In the sample below, you need to keep the if variable somewhere in your program and use it after :
FREE_IMAGE_FORMAT fif = FreeImage_RegisterLocalPlugin(InitMyPlugin);
assert(fif == FreeImage_GetFIFFromFilename( xxx ));
Ok so you only replied to a part of my comment. I will now avoid writing much, and try to make myself as clear as possible :)
Yes of course, you CAN'T use the original FIF anymore :)
IMO, from a design point of view, that's a shame. And it leads to ugly client code.
=> Let me know if you are open to the discussion.
Still, calling FreeImage_RegisterLocalPlugin( xxx ); for a natively supported format will FAIL.
=> I guess I should open an bug ticket then, right ?
It appears to me that it should be possible at all times to register a plugin that covers a format an already existing plugin also covers. There's no reason to disallow this. When the user calls FreeImage_GetFIFFromFormat() and there are two enabled plugins handling the format, FreeImage will return the FIF from the first which probably will be the internal one. Otherwise it will return the new one. I can't remember why I programmed it that way, but it seems nonsensical.
In other words, the line
if (FindNodeFromFormat(the_format) == NULL) {
should be deleted
Ogre3D use its own DDS plugin, together with FreeImage plugins, see :
https://bitbucket.org/sinbad/ogre/src/3a7079ea6c89/OgreMain/src/OgreDDSCodec.cpp
https://bitbucket.org/sinbad/ogre/src/3a7079ea6c89/OgreMain/src/OgreFreeImageCodec.cpp
LibPGF does the same using its own PNM plugin, see CImage::RegisterPNM in :
http://libpgf.svn.sourceforge.net/viewvc/libpgf/pgf/src/CImage.cpp?revision=42&view=markup
http://libpgf.svn.sourceforge.net/viewvc/libpgf/pgf/src/PNMPlugin.cpp?revision=45&view=markup
There's no need to change anything to my mind.
Hervé
I really wonder how LibPGF and Ogre3D add their own plugins without them modifying FreeImage. I'm analysing the source and it just can't work. Every plugin-add-function eventually calls PluginList::AddNode. In AddNode i see:
if (FindNodeFromFormat(the_format) == NULL) {
This line checks if any plugin with the same format string already exists and if yes, it skips the entire 'adding the plugin to the internal m_plugin_map' section. The entire plugin is never added.
On further inspection I tend to agree with Herve that probably no change is necessary in how the plugin system works per se. You can replace a plugin just fine as long as you give the new plugin a different format string.
This line: FreeImage_RegisterLocalPlugin( InitMyPlugin, "MY_DDS" ); is perfectly fine. FreeImage_GetFIFFromFilename() will take the disabled internal plugin into account and use the correct one. Alexandretrog is wrong in saying that it won't. I can clearly see from the source that it's correct.
However, I do still see some problems in the consistency of how FreeImage handles enabled/disabled plugins. For example PluginList::FindNodeFromMime() will return the first matching plugin encountered. If that one happens to be disabled, it will return FIF_UNKNOWN instead of looking for other matches.
Also I wonder about the use of enabled/disabled when a plugin actually loads an image. At this moment a disabled plugin will not load an image, even when the user deliberately chooses the plugin's FIF. I think enabled/disabled should be restricted to finding the plugin only.
What do you guys think?
Proposal for changes in Plugin.cpp
I attached a proposal for some changes in Plugin.cpp. The changes are:
! Changed FindNodeFromFormat and FindNodeFromMime to keep searching for plugins when the found one is disabled
Hi Floris,
Your patch is now added to the CVS.
Hervé