📄 localplugin.cs
字号:
protected virtual bool ValidateProc(ref FreeImageIO io, fi_handle handle) { return false; }
/// <summary>
/// Function that can be implemented.
/// </summary>
protected virtual string MimeProc() { return ""; }
/// <summary>
/// Function that can be implemented.
/// </summary>
protected virtual bool SupportsExportBPPProc(int bpp) { return false; }
/// <summary>
/// Function that can be implemented.
/// </summary>
protected virtual bool SupportsExportTypeProc(FREE_IMAGE_TYPE type) { return false; }
/// <summary>
/// Function that can be implemented.
/// </summary>
protected virtual bool SupportsICCProfilesProc() { return false; }
/// <summary>
/// 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.
/// </summary>
public LocalPlugin()
{
int i = 0;
implementedMethods = GetImplementedMethods();
if ((implementedMethods & MethodFlags.DescriptionProc) != 0)
{
plugin.descriptionProc = new DescriptionProc(DescriptionProc);
handles[i++] = GetHandle(plugin.descriptionProc);
}
if ((implementedMethods & MethodFlags.ExtensionListProc) != 0)
{
plugin.extensionListProc = new ExtensionListProc(ExtensionListProc);
handles[i++] = GetHandle(plugin.extensionListProc);
}
if ((implementedMethods & MethodFlags.RegExprProc) != 0)
{
plugin.regExprProc = new RegExprProc(RegExprProc);
handles[i++] = GetHandle(plugin.regExprProc);
}
if ((implementedMethods & MethodFlags.OpenProc) != 0)
{
plugin.openProc = new OpenProc(OpenProc);
handles[i++] = GetHandle(plugin.openProc);
}
if ((implementedMethods & MethodFlags.CloseProc) != 0)
{
plugin.closeProc = new CloseProc(CloseProc);
handles[i++] = GetHandle(plugin.closeProc);
}
if ((implementedMethods & MethodFlags.PageCountProc) != 0)
{
plugin.pageCountProc = new PageCountProc(PageCountProc);
handles[i++] = GetHandle(plugin.pageCountProc);
}
if ((implementedMethods & MethodFlags.PageCapabilityProc) != 0)
{
plugin.pageCapabilityProc = new PageCapabilityProc(PageCapabilityProc);
handles[i++] = GetHandle(plugin.pageCapabilityProc);
}
if ((implementedMethods & MethodFlags.LoadProc) != 0)
{
plugin.loadProc = new LoadProc(LoadProc);
handles[i++] = GetHandle(plugin.loadProc);
}
if ((implementedMethods & MethodFlags.SaveProc) != 0)
{
plugin.saveProc = new SaveProc(SaveProc);
handles[i++] = GetHandle(plugin.saveProc);
}
if ((implementedMethods & MethodFlags.ValidateProc) != 0)
{
plugin.validateProc = new ValidateProc(ValidateProc);
handles[i++] = GetHandle(plugin.validateProc);
}
if ((implementedMethods & MethodFlags.MimeProc) != 0)
{
plugin.mimeProc = new MimeProc(MimeProc);
handles[i++] = GetHandle(plugin.mimeProc);
}
if ((implementedMethods & MethodFlags.SupportsExportBPPProc) != 0)
{
plugin.supportsExportBPPProc = new SupportsExportBPPProc(SupportsExportBPPProc);
handles[i++] = GetHandle(plugin.supportsExportBPPProc);
}
if ((implementedMethods & MethodFlags.SupportsExportTypeProc) != 0)
{
plugin.supportsExportTypeProc = new SupportsExportTypeProc(SupportsExportTypeProc);
handles[i++] = GetHandle(plugin.supportsExportTypeProc);
}
if ((implementedMethods & MethodFlags.SupportsICCProfilesProc) != 0)
{
plugin.supportsICCProfilesProc = new SupportsICCProfilesProc(SupportsICCProfilesProc);
handles[i++] = GetHandle(plugin.supportsICCProfilesProc);
}
// FormatProc is always implemented
plugin.formatProc = new FormatProc(FormatProc);
handles[i++] = GetHandle(plugin.formatProc);
// InitProc is the register call back.
initProc = new InitProc(RegisterProc);
handles[i++] = GetHandle(initProc);
// 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);
}
}
/// <summary>
/// Releases all resources used by the instance.
/// </summary>
~LocalPlugin()
{
for (int i = 0; i < handles.Length; i++)
{
if (handles[i].IsAllocated)
{
handles[i].Free();
}
}
}
private GCHandle GetHandle(Delegate d)
{
return GCHandle.Alloc(d, GCHandleType.Normal);
}
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;
}
/// <summary>
/// Gets or sets if the plugin is enabled.
/// </summary>
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");
}
}
}
/// <summary>
/// Gets if the plugin was registered successfully.
/// </summary>
public bool Registered
{
get { return registered; }
}
/// <summary>
/// Gets the <see cref="FREE_IMAGE_FORMAT"/> FreeImage assigned to this plugin.
/// </summary>
public FREE_IMAGE_FORMAT Format
{
get
{
return format;
}
}
/// <summary>
/// Reads from an unmanaged stream.
/// </summary>
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);
}
}
/// <summary>
/// Reads a single byte from an unmanaged stream.
/// </summary>
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;
}
/// <summary>
/// Writes to an unmanaged stream.
/// </summary>
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);
}
}
/// <summary>
/// Writes a single byte to an unmanaged stream.
/// </summary>
protected unsafe int WriteByte(FreeImageIO io, fi_handle handle, byte value)
{
return (int)io.writeProc(new IntPtr(&value), 1, 1, handle);
}
/// <summary>
/// Seeks in an unmanaged stream.
/// </summary>
protected int Seek(FreeImageIO io, fi_handle handle, int offset, SeekOrigin origin)
{
return io.seekProc(handle, offset, origin);
}
/// <summary>
/// Retrieves the position of an unmanaged stream.
/// </summary>
protected int Tell(FreeImageIO io, fi_handle handle)
{
return io.tellProc(handle);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -