1
// ==========================================================
2
// FreeImage 3 .NET wrapper
3
// Original FreeImage 3 functions and .NET compatible derived functions
5
// Design and implementation by
6
// - Jean-Philippe Goerke (jpgoerke@users.sourceforge.net)
7
// - Carsten Klein (cklein05@users.sourceforge.net)
10
// - David Boland (davidboland@vodafone.ie)
12
// Main reference : MSDN Knowlede Base
14
// This file is part of FreeImage 3
16
// COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY
17
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES
18
// THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE
19
// OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED
20
// CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT
21
// THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY
22
// SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL
23
// PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER
26
// Use at your own risk!
27
// ==========================================================
29
// ==========================================================
32
// $Date: 2009/09/15 11:47:46 $
33
// $Id: LocalPlugin.cs,v 1.9 2009/09/15 11:47:46 cklein05 Exp $
34
// ==========================================================
38
using System.Runtime.InteropServices;
39
using FreeImageAPI.IO;
40
using System.Diagnostics;
42
namespace FreeImageAPI.Plugins
45
/// Class representing own FreeImage-Plugins.
48
/// FreeImages itself is plugin based. Each supported format is integrated by a seperat plugin,
49
/// that handles loading, saving, descriptions, identifing ect.
50
/// And of course the user can create own plugins and use them in FreeImage.
51
/// To do that the above mentioned predefined methodes need to be implemented.
53
/// The class below handles the creation of such a plugin. The class itself is abstract
54
/// as well as some core functions that need to be implemented.
55
/// The class can be used to enable or disable the plugin in FreeImage after regististration or
56
/// retrieve the formatid, assigned by FreeImage.
57
/// The class handles the callback functions, garbage collector and pointer operation to make
58
/// the implementation as user friendly as possible.
61
/// There are two functions that need to be implemented:
62
/// <see cref="FreeImageAPI.Plugins.LocalPlugin.GetImplementedMethods"/> and
63
/// <see cref="FreeImageAPI.Plugins.LocalPlugin.FormatProc"/>.
64
/// <see cref="FreeImageAPI.Plugins.LocalPlugin.GetImplementedMethods"/> is used by the constructor
65
/// of the abstract class. FreeImage wants a list of the implemented functions. Each function is
66
/// represented by a function pointer (a .NET <see cref="System.Delegate"/>). In case a function
67
/// is not implemented FreeImage receives an empty <b>delegate</b>). To tell the constructor
68
/// which functions have been implemented the information is represented by a disjunction of
69
/// <see cref="FreeImageAPI.Plugins.LocalPlugin.MethodFlags"/>.
72
/// return MethodFlags.LoadProc | MethodFlags.SaveProc;
74
/// The above statement means that LoadProc and SaveProc have been implemented by the user.
75
/// Keep in mind, that each function has a standard implementation that has static return
76
/// values that may cause errors if listed in
77
/// <see cref="FreeImageAPI.Plugins.LocalPlugin.GetImplementedMethods"/> without a real implementation.
79
/// <see cref="FreeImageAPI.Plugins.LocalPlugin.FormatProc"/> is used by some checks of FreeImage and
80
/// must be implemented. <see cref="FreeImageAPI.Plugins.LocalPlugin.LoadProc"/> for example can be
81
/// implemented if the plugin supports reading, but it doesn't have to, the plugin could only
82
/// be used to save an already loaded bitmap in a special format.
84
public abstract class LocalPlugin
87
/// Struct containing function pointers.
89
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
90
private Plugin plugin;
93
/// Delegate for register callback by FreeImage.
95
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
96
private InitProc initProc;
99
/// The format id assiged to the plugin.
101
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
102
protected FREE_IMAGE_FORMAT format = FREE_IMAGE_FORMAT.FIF_UNKNOWN;
105
/// When true the plugin was registered successfully else false.
107
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
108
protected readonly bool registered = false;
111
/// A copy of the functions used to register.
113
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
114
protected readonly MethodFlags implementedMethods;
117
/// MethodFlags defines values to fill a bitfield telling which
118
/// functions have been implemented by a plugin.
121
protected enum MethodFlags
124
/// No mothods implemented.
129
/// DescriptionProc has been implemented.
131
DescriptionProc = 0x1,
134
/// ExtensionListProc has been implemented.
136
ExtensionListProc = 0x2,
139
/// RegExprProc has been implemented.
144
/// OpenProc has been implemented.
149
/// CloseProc has been implemented.
154
/// PageCountProc has been implemented.
156
PageCountProc = 0x20,
159
/// PageCapabilityProc has been implemented.
161
PageCapabilityProc = 0x40,
164
/// LoadProc has been implemented.
169
/// SaveProc has been implemented.
174
/// ValidateProc has been implemented.
176
ValidateProc = 0x200,
179
/// MimeProc has been implemented.
184
/// SupportsExportBPPProc has been implemented.
186
SupportsExportBPPProc = 0x800,
189
/// SupportsExportTypeProc has been implemented.
191
SupportsExportTypeProc = 0x1000,
194
/// SupportsICCProfilesProc has been implemented.
196
SupportsICCProfilesProc = 0x2000
199
// Functions that must be implemented.
202
/// Function that returns a bitfield containing the
203
/// implemented methods.
205
/// <returns>Bitfield of the implemented methods.</returns>
206
protected abstract MethodFlags GetImplementedMethods();
209
/// Implementation of <b>FormatProc</b>
211
/// <returns>A string containing the plugins format.</returns>
212
protected abstract string FormatProc();
214
// Functions that can be implemented.
217
/// Function that can be implemented.
219
protected virtual string DescriptionProc() { return ""; }
221
/// Function that can be implemented.
223
protected virtual string ExtensionListProc() { return ""; }
225
/// Function that can be implemented.
227
protected virtual string RegExprProc() { return ""; }
229
/// Function that can be implemented.
231
protected virtual IntPtr OpenProc(ref FreeImageIO io, fi_handle handle, bool read) { return IntPtr.Zero; }
233
/// Function that can be implemented.
235
protected virtual void CloseProc(ref FreeImageIO io, fi_handle handle, IntPtr data) { }
237
/// Function that can be implemented.
239
protected virtual int PageCountProc(ref FreeImageIO io, fi_handle handle, IntPtr data) { return 0; }
241
/// Function that can be implemented.
243
protected virtual int PageCapabilityProc(ref FreeImageIO io, fi_handle handle, IntPtr data) { return 0; }
245
/// Function that can be implemented.
247
protected virtual FIBITMAP LoadProc(ref FreeImageIO io, fi_handle handle, int page, int flags, IntPtr data) { return FIBITMAP.Zero; }
249
/// Function that can be implemented.
251
protected virtual bool SaveProc(ref FreeImageIO io, FIBITMAP dib, fi_handle handle, int page, int flags, IntPtr data) { return false; }
253
/// Function that can be implemented.
255
protected virtual bool ValidateProc(ref FreeImageIO io, fi_handle handle) { return false; }
257
/// Function that can be implemented.
259
protected virtual string MimeProc() { return ""; }
261
/// Function that can be implemented.
263
protected virtual bool SupportsExportBPPProc(int bpp) { return false; }
265
/// Function that can be implemented.
267
protected virtual bool SupportsExportTypeProc(FREE_IMAGE_TYPE type) { return false; }
269
/// Function that can be implemented.
271
protected virtual bool SupportsICCProfilesProc() { return false; }
274
/// The constructor automatically registeres the plugin in FreeImage.
275
/// To do this it prepares a FreeImage defined structure with function pointers
276
/// to the implemented functions or null if not implemented.
277
/// Before registing the functions they are pinned in memory so the garbage collector
278
/// can't move them around in memory after we passed there addresses to FreeImage.
282
implementedMethods = GetImplementedMethods();
284
if ((implementedMethods & MethodFlags.DescriptionProc) != 0)
286
plugin.descriptionProc = new DescriptionProc(DescriptionProc);
288
if ((implementedMethods & MethodFlags.ExtensionListProc) != 0)
290
plugin.extensionListProc = new ExtensionListProc(ExtensionListProc);
292
if ((implementedMethods & MethodFlags.RegExprProc) != 0)
294
plugin.regExprProc = new RegExprProc(RegExprProc);
296
if ((implementedMethods & MethodFlags.OpenProc) != 0)
298
plugin.openProc = new OpenProc(OpenProc);
300
if ((implementedMethods & MethodFlags.CloseProc) != 0)
302
plugin.closeProc = new CloseProc(CloseProc);
304
if ((implementedMethods & MethodFlags.PageCountProc) != 0)
306
plugin.pageCountProc = new PageCountProc(PageCountProc);
308
if ((implementedMethods & MethodFlags.PageCapabilityProc) != 0)
310
plugin.pageCapabilityProc = new PageCapabilityProc(PageCapabilityProc);
312
if ((implementedMethods & MethodFlags.LoadProc) != 0)
314
plugin.loadProc = new LoadProc(LoadProc);
316
if ((implementedMethods & MethodFlags.SaveProc) != 0)
318
plugin.saveProc = new SaveProc(SaveProc);
320
if ((implementedMethods & MethodFlags.ValidateProc) != 0)
322
plugin.validateProc = new ValidateProc(ValidateProc);
324
if ((implementedMethods & MethodFlags.MimeProc) != 0)
326
plugin.mimeProc = new MimeProc(MimeProc);
328
if ((implementedMethods & MethodFlags.SupportsExportBPPProc) != 0)
330
plugin.supportsExportBPPProc = new SupportsExportBPPProc(SupportsExportBPPProc);
332
if ((implementedMethods & MethodFlags.SupportsExportTypeProc) != 0)
334
plugin.supportsExportTypeProc = new SupportsExportTypeProc(SupportsExportTypeProc);
336
if ((implementedMethods & MethodFlags.SupportsICCProfilesProc) != 0)
338
plugin.supportsICCProfilesProc = new SupportsICCProfilesProc(SupportsICCProfilesProc);
341
// FormatProc is always implemented
342
plugin.formatProc = new FormatProc(FormatProc);
344
// InitProc is the register call back.
345
initProc = new InitProc(RegisterProc);
347
// Register the plugin. The result will be saved and can be accessed later.
348
registered = FreeImage.RegisterLocalPlugin(initProc, null, null, null, null) != FREE_IMAGE_FORMAT.FIF_UNKNOWN;
351
PluginRepository.RegisterLocalPlugin(this);
355
private void RegisterProc(ref Plugin plugin, int format_id)
357
// Copy the function pointers
358
plugin = this.plugin;
359
// Retrieve the format if assigned to this plugin by FreeImage.
360
format = (FREE_IMAGE_FORMAT)format_id;
364
/// Gets or sets if the plugin is enabled.
372
return (FreeImage.IsPluginEnabled(format) > 0);
376
throw new ObjectDisposedException("plugin not registered");
383
FreeImage.SetPluginEnabled(format, value);
387
throw new ObjectDisposedException("plugin not registered");
393
/// Gets if the plugin was registered successfully.
395
public bool Registered
397
get { return registered; }
401
/// Gets the <see cref="FREE_IMAGE_FORMAT"/> FreeImage assigned to this plugin.
403
public FREE_IMAGE_FORMAT Format
412
/// Reads from an unmanaged stream.
414
protected unsafe int Read(FreeImageIO io, fi_handle handle, uint size, uint count, ref byte[] buffer)
416
fixed (byte* ptr = buffer)
418
return (int)io.readProc(new IntPtr(ptr), size, count, handle);
423
/// Reads a single byte from an unmanaged stream.
425
protected unsafe int ReadByte(FreeImageIO io, fi_handle handle)
428
return (int)io.readProc(new IntPtr(&buffer), 1, 1, handle) > 0 ? buffer : -1;
432
/// Writes to an unmanaged stream.
434
protected unsafe int Write(FreeImageIO io, fi_handle handle, uint size, uint count, ref byte[] buffer)
436
fixed (byte* ptr = buffer)
438
return (int)io.writeProc(new IntPtr(ptr), size, count, handle);
443
/// Writes a single byte to an unmanaged stream.
445
protected unsafe int WriteByte(FreeImageIO io, fi_handle handle, byte value)
447
return (int)io.writeProc(new IntPtr(&value), 1, 1, handle);
451
/// Seeks in an unmanaged stream.
453
protected int Seek(FreeImageIO io, fi_handle handle, int offset, SeekOrigin origin)
455
return io.seekProc(handle, offset, origin);
459
/// Retrieves the position of an unmanaged stream.
461
protected int Tell(FreeImageIO io, fi_handle handle)
463
return io.tellProc(handle);
b'\\ No newline at end of file'