// ========================================================== // FreeImage 3 .NET wrapper // Original FreeImage 3 functions and .NET compatible derived functions // // Design and implementation by // - Jean-Philippe Goerke (jpgoerke@users.sourceforge.net) // - Carsten Klein (cklein05@users.sourceforge.net) // // Contributors: // - David Boland (davidboland@vodafone.ie) // // Main reference : MSDN Knowlede Base // // This file is part of FreeImage 3 // // COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY // OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES // THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE // OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED // CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT // THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY // SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL // PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER // THIS DISCLAIMER. // // Use at your own risk! // ========================================================== // ========================================================== // CVS // $Revision: 1.9 $ // $Date: 2009/09/15 11:47:46 $ // $Id: LocalPlugin.cs,v 1.9 2009/09/15 11:47:46 cklein05 Exp $ // ========================================================== using System; using System.IO; using System.Runtime.InteropServices; using FreeImageAPI.IO; using System.Diagnostics; namespace FreeImageAPI.Plugins { /// /// Class representing own FreeImage-Plugins. /// /// /// FreeImages itself is plugin based. Each supported format is integrated by a seperat plugin, /// that handles loading, saving, descriptions, identifing ect. /// And of course the user can create own plugins and use them in FreeImage. /// To do that the above mentioned predefined methodes need to be implemented. /// /// The class below handles the creation of such a plugin. The class itself is abstract /// as well as some core functions that need to be implemented. /// The class can be used to enable or disable the plugin in FreeImage after regististration or /// retrieve the formatid, assigned by FreeImage. /// The class handles the callback functions, garbage collector and pointer operation to make /// the implementation as user friendly as possible. /// /// How to: /// There are two functions that need to be implemented: /// and /// . /// is used by the constructor /// of the abstract class. FreeImage wants a list of the implemented functions. Each function is /// represented by a function pointer (a .NET ). In case a function /// is not implemented FreeImage receives an empty delegate). To tell the constructor /// which functions have been implemented the information is represented by a disjunction of /// . /// /// For example: /// return MethodFlags.LoadProc | MethodFlags.SaveProc; /// /// The above statement means that LoadProc and SaveProc have been implemented by the user. /// Keep in mind, that each function has a standard implementation that has static return /// values that may cause errors if listed in /// without a real implementation. /// /// is used by some checks of FreeImage and /// must be implemented. for example can be /// implemented if the plugin supports reading, but it doesn't have to, the plugin could only /// be used to save an already loaded bitmap in a special format. /// public abstract class LocalPlugin { /// /// Struct containing function pointers. /// [DebuggerBrowsable(DebuggerBrowsableState.Never)] private Plugin plugin; /// /// Delegate for register callback by FreeImage. /// [DebuggerBrowsable(DebuggerBrowsableState.Never)] private InitProc initProc; /// /// The format id assiged to the plugin. /// [DebuggerBrowsable(DebuggerBrowsableState.Never)] protected FREE_IMAGE_FORMAT format = FREE_IMAGE_FORMAT.FIF_UNKNOWN; /// /// When true the plugin was registered successfully else false. /// [DebuggerBrowsable(DebuggerBrowsableState.Never)] protected readonly bool registered = false; /// /// A copy of the functions used to register. /// [DebuggerBrowsable(DebuggerBrowsableState.Never)] protected readonly MethodFlags implementedMethods; /// /// MethodFlags defines values to fill a bitfield telling which /// functions have been implemented by a plugin. /// [Flags] protected enum MethodFlags { /// /// No mothods implemented. /// None = 0x0, /// /// DescriptionProc has been implemented. /// DescriptionProc = 0x1, /// /// ExtensionListProc has been implemented. /// ExtensionListProc = 0x2, /// /// RegExprProc has been implemented. /// RegExprProc = 0x4, /// /// OpenProc has been implemented. /// OpenProc = 0x8, /// /// CloseProc has been implemented. /// CloseProc = 0x10, /// /// PageCountProc has been implemented. /// PageCountProc = 0x20, /// /// PageCapabilityProc has been implemented. /// PageCapabilityProc = 0x40, /// /// LoadProc has been implemented. /// LoadProc = 0x80, /// /// SaveProc has been implemented. /// SaveProc = 0x100, /// /// ValidateProc has been implemented. /// ValidateProc = 0x200, /// /// MimeProc has been implemented. /// MimeProc = 0x400, /// /// SupportsExportBPPProc has been implemented. /// SupportsExportBPPProc = 0x800, /// /// SupportsExportTypeProc has been implemented. /// SupportsExportTypeProc = 0x1000, /// /// SupportsICCProfilesProc has been implemented. /// SupportsICCProfilesProc = 0x2000 } // Functions that must be implemented. /// /// Function that returns a bitfield containing the /// implemented methods. /// /// Bitfield of the implemented methods. protected abstract MethodFlags GetImplementedMethods(); /// /// Implementation of FormatProc /// /// A string containing the plugins format. protected abstract string FormatProc(); // Functions that can be implemented. /// /// Function that can be implemented. /// protected virtual string DescriptionProc() { return ""; } /// /// Function that can be implemented. /// protected virtual string ExtensionListProc() { return ""; } /// /// Function that can be implemented. /// protected virtual string RegExprProc() { return ""; } /// /// Function that can be implemented. /// protected virtual IntPtr OpenProc(ref FreeImageIO io, fi_handle handle, bool read) { return IntPtr.Zero; } /// /// Function that can be implemented. /// protected virtual void CloseProc(ref FreeImageIO io, fi_handle handle, IntPtr data) { } /// /// Function that can be implemented. /// protected virtual int PageCountProc(ref FreeImageIO io, fi_handle handle, IntPtr data) { return 0; } /// /// Function that can be implemented. /// protected virtual int PageCapabilityProc(ref FreeImageIO io, fi_handle handle, IntPtr data) { return 0; } /// /// Function that can be implemented. /// protected virtual FIBITMAP LoadProc(ref FreeImageIO io, fi_handle handle, int page, int flags, IntPtr data) { return FIBITMAP.Zero; } /// /// Function that can be implemented. /// protected virtual bool SaveProc(ref FreeImageIO io, FIBITMAP dib, fi_handle handle, int page, int flags, IntPtr data) { return false; } /// /// Function that can be implemented. /// protected virtual bool ValidateProc(ref FreeImageIO io, fi_handle handle) { return false; } /// /// Function that can be implemented. /// protected virtual string MimeProc() { return ""; } /// /// Function that can be implemented. /// protected virtual bool SupportsExportBPPProc(int bpp) { return false; } /// /// Function that can be implemented. /// protected virtual bool SupportsExportTypeProc(FREE_IMAGE_TYPE type) { return false; } /// /// Function that can be implemented. /// protected virtual bool SupportsICCProfilesProc() { return false; } /// /// The constructor automatically registeres the plugin in FreeImage. /// To do this it prepares a FreeImage defined structure with function pointers /// to the implemented functions or null if not implemented. /// Before registing the functions they are pinned in memory so the garbage collector /// can't move them around in memory after we passed there addresses to FreeImage. /// public LocalPlugin() { implementedMethods = GetImplementedMethods(); if ((implementedMethods & MethodFlags.DescriptionProc) != 0) { plugin.descriptionProc = new DescriptionProc(DescriptionProc); } if ((implementedMethods & MethodFlags.ExtensionListProc) != 0) { plugin.extensionListProc = new ExtensionListProc(ExtensionListProc); } if ((implementedMethods & MethodFlags.RegExprProc) != 0) { plugin.regExprProc = new RegExprProc(RegExprProc); } if ((implementedMethods & MethodFlags.OpenProc) != 0) { plugin.openProc = new OpenProc(OpenProc); } if ((implementedMethods & MethodFlags.CloseProc) != 0) { plugin.closeProc = new CloseProc(CloseProc); } if ((implementedMethods & MethodFlags.PageCountProc) != 0) { plugin.pageCountProc = new PageCountProc(PageCountProc); } if ((implementedMethods & MethodFlags.PageCapabilityProc) != 0) { plugin.pageCapabilityProc = new PageCapabilityProc(PageCapabilityProc); } if ((implementedMethods & MethodFlags.LoadProc) != 0) { plugin.loadProc = new LoadProc(LoadProc); } if ((implementedMethods & MethodFlags.SaveProc) != 0) { plugin.saveProc = new SaveProc(SaveProc); } if ((implementedMethods & MethodFlags.ValidateProc) != 0) { plugin.validateProc = new ValidateProc(ValidateProc); } if ((implementedMethods & MethodFlags.MimeProc) != 0) { plugin.mimeProc = new MimeProc(MimeProc); } if ((implementedMethods & MethodFlags.SupportsExportBPPProc) != 0) { plugin.supportsExportBPPProc = new SupportsExportBPPProc(SupportsExportBPPProc); } if ((implementedMethods & MethodFlags.SupportsExportTypeProc) != 0) { plugin.supportsExportTypeProc = new SupportsExportTypeProc(SupportsExportTypeProc); } if ((implementedMethods & MethodFlags.SupportsICCProfilesProc) != 0) { plugin.supportsICCProfilesProc = new SupportsICCProfilesProc(SupportsICCProfilesProc); } // FormatProc is always implemented plugin.formatProc = new FormatProc(FormatProc); // InitProc is the register call back. initProc = new InitProc(RegisterProc); // Register the plugin. The result will be saved and can be accessed later. registered = FreeImage.RegisterLocalPlugin(initProc, null, null, null, null) != FREE_IMAGE_FORMAT.FIF_UNKNOWN; if (registered) { PluginRepository.RegisterLocalPlugin(this); } } private void RegisterProc(ref Plugin plugin, int format_id) { // Copy the function pointers plugin = this.plugin; // Retrieve the format if assigned to this plugin by FreeImage. format = (FREE_IMAGE_FORMAT)format_id; } /// /// Gets or sets if the plugin is enabled. /// public bool Enabled { get { if (registered) { return (FreeImage.IsPluginEnabled(format) > 0); } else { throw new ObjectDisposedException("plugin not registered"); } } set { if (registered) { FreeImage.SetPluginEnabled(format, value); } else { throw new ObjectDisposedException("plugin not registered"); } } } /// /// Gets if the plugin was registered successfully. /// public bool Registered { get { return registered; } } /// /// Gets the FreeImage assigned to this plugin. /// public FREE_IMAGE_FORMAT Format { get { return format; } } /// /// Reads from an unmanaged stream. /// protected unsafe int Read(FreeImageIO io, fi_handle handle, uint size, uint count, ref byte[] buffer) { fixed (byte* ptr = buffer) { return (int)io.readProc(new IntPtr(ptr), size, count, handle); } } /// /// Reads a single byte from an unmanaged stream. /// protected unsafe int ReadByte(FreeImageIO io, fi_handle handle) { byte buffer = 0; return (int)io.readProc(new IntPtr(&buffer), 1, 1, handle) > 0 ? buffer : -1; } /// /// Writes to an unmanaged stream. /// protected unsafe int Write(FreeImageIO io, fi_handle handle, uint size, uint count, ref byte[] buffer) { fixed (byte* ptr = buffer) { return (int)io.writeProc(new IntPtr(ptr), size, count, handle); } } /// /// Writes a single byte to an unmanaged stream. /// protected unsafe int WriteByte(FreeImageIO io, fi_handle handle, byte value) { return (int)io.writeProc(new IntPtr(&value), 1, 1, handle); } /// /// Seeks in an unmanaged stream. /// protected int Seek(FreeImageIO io, fi_handle handle, int offset, SeekOrigin origin) { return io.seekProc(handle, offset, origin); } /// /// Retrieves the position of an unmanaged stream. /// protected int Tell(FreeImageIO io, fi_handle handle) { return io.tellProc(handle); } } }