Menu ▾ ▴

#76 Override builtin Plugins with a Local (or external) one

None
closed
None
5
2013-01-09
2012-02-23
No

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,

  • either add a parameter " bool _bOverridePrevious " to FreeImage_RegisterLocalPlugin
  • or add a specific function "FreeImage_ReplacePlugin( xxx )"

Discussion

  • Hervé Drolon

    Hervé Drolon - 2012-02-23

    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);
    }

     
  • Alexandre TROG

    Alexandre TROG - 2012-02-24

    Unfortunately, no, your function RegisterMyPlugin() is not working. Here is why :

    If you call :

    FreeImage_RegisterLocalPlugin( xxx );
    

    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;

                m_plugin_map[(const int)m_plugin_map.size()] = node;
    
                return (FREE_IMAGE_FORMAT)node->m_id;
            }
        }
    
        // something went wrong while allocating the plugin... cleanup
    
                 ////////////// YUP, something went wrong :)
    

    }}}

    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

     FreeImage_RegisterLocalPlugin( InitMyPlugin, "SOMETHINGELSE" );
    
     FreeImage_SetPluginEnabled(FIF_MY, FALSE);
    

    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 ).

     
  • Alexandre TROG

    Alexandre TROG - 2012-02-24

    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 ).

     
  • Hervé Drolon

    Hervé Drolon - 2012-02-26

    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 ));

     
  • Alexandre TROG

    Alexandre TROG - 2012-02-26

    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 :)

    1. 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.

    2. Still, calling FreeImage_RegisterLocalPlugin( xxx ); for a natively supported format will FAIL.
      => I guess I should open an bug ticket then, right ?

     
  • Floris van den Berg

    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.

     
  • Floris van den Berg

    In other words, the line

    if (FindNodeFromFormat(the_format) == NULL) {

    should be deleted

     
  • Floris van den Berg

    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.

     
  • Floris van den Berg

    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?

     
  • Floris van den Berg

    Proposal for changes in Plugin.cpp

     
  • Floris van den Berg

    I attached a proposal for some changes in Plugin.cpp. The changes are:

    • Removed check if a plugin with a duplicate format string already exists when adding a plugin
      ! Changed FindNodeFromFormat and FindNodeFromMime to keep searching for plugins when the found one is disabled
     
  • Hervé Drolon

    Hervé Drolon - 2012-07-26

    Hi Floris,

    Your patch is now added to the CVS.

    Hervé

     
  • Hervé Drolon

    Hervé Drolon - 2013-01-09
    • status: pending --> closed
    • milestone: -->
     

Anonymous
Anonymous

Add attachments
Cancel