~cosme/ubuntu/precise/freeimage/freeimage-3.15.1

« back to all changes in this revision

Viewing changes to Wrapper/FreeImage.NET/cs/Library/Classes/LocalPlugin.cs

  • Committer: Bazaar Package Importer
  • Author(s): Cosme Domínguez Díaz
  • Date: 2010-07-20 13:42:15 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20100720134215-xt1454zaedv3b604
Tags: 3.13.1-0ubuntu1
* New upstream release. Closes: (LP: #607800)
 - Updated debian/freeimage-get-orig-source script.
 - Removing no longer necessary debian/patches/* and
   the patch system in debian/rules.
 - Updated debian/rules to work with the new Makefiles.
 - Drop from -O3 to -O2 and use lzma compression saves
   ~10 MB of free space. 
* lintian stuff
 - fixed debhelper-but-no-misc-depends
 - fixed ldconfig-symlink-missing-for-shlib

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// ==========================================================
 
2
// FreeImage 3 .NET wrapper
 
3
// Original FreeImage 3 functions and .NET compatible derived functions
 
4
//
 
5
// Design and implementation by
 
6
// - Jean-Philippe Goerke (jpgoerke@users.sourceforge.net)
 
7
// - Carsten Klein (cklein05@users.sourceforge.net)
 
8
//
 
9
// Contributors:
 
10
// - David Boland (davidboland@vodafone.ie)
 
11
//
 
12
// Main reference : MSDN Knowlede Base
 
13
//
 
14
// This file is part of FreeImage 3
 
15
//
 
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
 
24
// THIS DISCLAIMER.
 
25
//
 
26
// Use at your own risk!
 
27
// ==========================================================
 
28
 
 
29
// ==========================================================
 
30
// CVS
 
31
// $Revision: 1.9 $
 
32
// $Date: 2009/09/15 11:47:46 $
 
33
// $Id: LocalPlugin.cs,v 1.9 2009/09/15 11:47:46 cklein05 Exp $
 
34
// ==========================================================
 
35
 
 
36
using System;
 
37
using System.IO;
 
38
using System.Runtime.InteropServices;
 
39
using FreeImageAPI.IO;
 
40
using System.Diagnostics;
 
41
 
 
42
namespace FreeImageAPI.Plugins
 
43
{
 
44
        /// <summary>
 
45
        /// Class representing own FreeImage-Plugins.
 
46
        /// </summary>
 
47
        /// <remarks>
 
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.
 
52
        /// <para/>
 
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.
 
59
        /// <para/>
 
60
        /// How to:
 
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"/>.
 
70
        /// <para/>
 
71
        /// For example:
 
72
        ///             return MethodFlags.LoadProc | MethodFlags.SaveProc;
 
73
        /// <para/>
 
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.
 
78
        /// <para/>
 
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.
 
83
        /// </remarks>
 
84
        public abstract class LocalPlugin
 
85
        {
 
86
                /// <summary>
 
87
                /// Struct containing function pointers.
 
88
                /// </summary>
 
89
                [DebuggerBrowsable(DebuggerBrowsableState.Never)]
 
90
                private Plugin plugin;
 
91
 
 
92
                /// <summary>
 
93
                /// Delegate for register callback by FreeImage.
 
94
                /// </summary>
 
95
                [DebuggerBrowsable(DebuggerBrowsableState.Never)]
 
96
                private InitProc initProc;
 
97
 
 
98
                /// <summary>
 
99
                /// The format id assiged to the plugin.
 
100
                /// </summary>
 
101
                [DebuggerBrowsable(DebuggerBrowsableState.Never)]
 
102
                protected FREE_IMAGE_FORMAT format = FREE_IMAGE_FORMAT.FIF_UNKNOWN;
 
103
 
 
104
                /// <summary>
 
105
                /// When true the plugin was registered successfully else false.
 
106
                /// </summary>
 
107
                [DebuggerBrowsable(DebuggerBrowsableState.Never)]
 
108
                protected readonly bool registered = false;
 
109
 
 
110
                /// <summary>
 
111
                /// A copy of the functions used to register.
 
112
                /// </summary>
 
113
                [DebuggerBrowsable(DebuggerBrowsableState.Never)]
 
114
                protected readonly MethodFlags implementedMethods;
 
115
 
 
116
                /// <summary>
 
117
                /// MethodFlags defines values to fill a bitfield telling which
 
118
                /// functions have been implemented by a plugin.
 
119
                /// </summary>
 
120
                [Flags]
 
121
                protected enum MethodFlags
 
122
                {
 
123
                        /// <summary>
 
124
                        /// No mothods implemented.
 
125
                        /// </summary>
 
126
                        None = 0x0,
 
127
 
 
128
                        /// <summary>
 
129
                        /// DescriptionProc has been implemented.
 
130
                        /// </summary>
 
131
                        DescriptionProc = 0x1,
 
132
 
 
133
                        /// <summary>
 
134
                        /// ExtensionListProc has been implemented.
 
135
                        /// </summary>
 
136
                        ExtensionListProc = 0x2,
 
137
 
 
138
                        /// <summary>
 
139
                        /// RegExprProc has been implemented.
 
140
                        /// </summary>
 
141
                        RegExprProc = 0x4,
 
142
 
 
143
                        /// <summary>
 
144
                        /// OpenProc has been implemented.
 
145
                        /// </summary>
 
146
                        OpenProc = 0x8,
 
147
 
 
148
                        /// <summary>
 
149
                        /// CloseProc has been implemented.
 
150
                        /// </summary>
 
151
                        CloseProc = 0x10,
 
152
 
 
153
                        /// <summary>
 
154
                        /// PageCountProc has been implemented.
 
155
                        /// </summary>
 
156
                        PageCountProc = 0x20,
 
157
 
 
158
                        /// <summary>
 
159
                        /// PageCapabilityProc has been implemented.
 
160
                        /// </summary>
 
161
                        PageCapabilityProc = 0x40,
 
162
 
 
163
                        /// <summary>
 
164
                        /// LoadProc has been implemented.
 
165
                        /// </summary>
 
166
                        LoadProc = 0x80,
 
167
 
 
168
                        /// <summary>
 
169
                        /// SaveProc has been implemented.
 
170
                        /// </summary>
 
171
                        SaveProc = 0x100,
 
172
 
 
173
                        /// <summary>
 
174
                        /// ValidateProc has been implemented.
 
175
                        /// </summary>
 
176
                        ValidateProc = 0x200,
 
177
 
 
178
                        /// <summary>
 
179
                        /// MimeProc has been implemented.
 
180
                        /// </summary>
 
181
                        MimeProc = 0x400,
 
182
 
 
183
                        /// <summary>
 
184
                        /// SupportsExportBPPProc has been implemented.
 
185
                        /// </summary>
 
186
                        SupportsExportBPPProc = 0x800,
 
187
 
 
188
                        /// <summary>
 
189
                        /// SupportsExportTypeProc has been implemented.
 
190
                        /// </summary>
 
191
                        SupportsExportTypeProc = 0x1000,
 
192
 
 
193
                        /// <summary>
 
194
                        /// SupportsICCProfilesProc has been implemented.
 
195
                        /// </summary>
 
196
                        SupportsICCProfilesProc = 0x2000
 
197
                }
 
198
 
 
199
                // Functions that must be implemented.
 
200
 
 
201
                /// <summary>
 
202
                /// Function that returns a bitfield containing the
 
203
                /// implemented methods.
 
204
                /// </summary>
 
205
                /// <returns>Bitfield of the implemented methods.</returns>
 
206
                protected abstract MethodFlags GetImplementedMethods();
 
207
 
 
208
                /// <summary>
 
209
                /// Implementation of <b>FormatProc</b>
 
210
                /// </summary>
 
211
                /// <returns>A string containing the plugins format.</returns>
 
212
                protected abstract string FormatProc();
 
213
 
 
214
                // Functions that can be implemented.
 
215
 
 
216
                /// <summary>
 
217
                /// Function that can be implemented.
 
218
                /// </summary>
 
219
                protected virtual string DescriptionProc() { return ""; }
 
220
                /// <summary>
 
221
                /// Function that can be implemented.
 
222
                /// </summary>
 
223
                protected virtual string ExtensionListProc() { return ""; }
 
224
                /// <summary>
 
225
                /// Function that can be implemented.
 
226
                /// </summary>
 
227
                protected virtual string RegExprProc() { return ""; }
 
228
                /// <summary>
 
229
                /// Function that can be implemented.
 
230
                /// </summary>
 
231
                protected virtual IntPtr OpenProc(ref FreeImageIO io, fi_handle handle, bool read) { return IntPtr.Zero; }
 
232
                /// <summary>
 
233
                /// Function that can be implemented.
 
234
                /// </summary>
 
235
                protected virtual void CloseProc(ref FreeImageIO io, fi_handle handle, IntPtr data) { }
 
236
                /// <summary>
 
237
                /// Function that can be implemented.
 
238
                /// </summary>
 
239
                protected virtual int PageCountProc(ref FreeImageIO io, fi_handle handle, IntPtr data) { return 0; }
 
240
                /// <summary>
 
241
                /// Function that can be implemented.
 
242
                /// </summary>
 
243
                protected virtual int PageCapabilityProc(ref FreeImageIO io, fi_handle handle, IntPtr data) { return 0; }
 
244
                /// <summary>
 
245
                /// Function that can be implemented.
 
246
                /// </summary>
 
247
                protected virtual FIBITMAP LoadProc(ref FreeImageIO io, fi_handle handle, int page, int flags, IntPtr data) { return FIBITMAP.Zero; }
 
248
                /// <summary>
 
249
                /// Function that can be implemented.
 
250
                /// </summary>
 
251
                protected virtual bool SaveProc(ref FreeImageIO io, FIBITMAP dib, fi_handle handle, int page, int flags, IntPtr data) { return false; }
 
252
                /// <summary>
 
253
                /// Function that can be implemented.
 
254
                /// </summary>
 
255
                protected virtual bool ValidateProc(ref FreeImageIO io, fi_handle handle) { return false; }
 
256
                /// <summary>
 
257
                /// Function that can be implemented.
 
258
                /// </summary>
 
259
                protected virtual string MimeProc() { return ""; }
 
260
                /// <summary>
 
261
                /// Function that can be implemented.
 
262
                /// </summary>
 
263
                protected virtual bool SupportsExportBPPProc(int bpp) { return false; }
 
264
                /// <summary>
 
265
                /// Function that can be implemented.
 
266
                /// </summary>
 
267
                protected virtual bool SupportsExportTypeProc(FREE_IMAGE_TYPE type) { return false; }
 
268
                /// <summary>
 
269
                /// Function that can be implemented.
 
270
                /// </summary>
 
271
                protected virtual bool SupportsICCProfilesProc() { return false; }
 
272
 
 
273
                /// <summary>
 
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.
 
279
                /// </summary>
 
280
                public LocalPlugin()
 
281
                {
 
282
                        implementedMethods = GetImplementedMethods();
 
283
 
 
284
                        if ((implementedMethods & MethodFlags.DescriptionProc) != 0)
 
285
                        {
 
286
                                plugin.descriptionProc = new DescriptionProc(DescriptionProc);
 
287
                        }
 
288
                        if ((implementedMethods & MethodFlags.ExtensionListProc) != 0)
 
289
                        {
 
290
                                plugin.extensionListProc = new ExtensionListProc(ExtensionListProc);
 
291
                        }
 
292
                        if ((implementedMethods & MethodFlags.RegExprProc) != 0)
 
293
                        {
 
294
                                plugin.regExprProc = new RegExprProc(RegExprProc);
 
295
                        }
 
296
                        if ((implementedMethods & MethodFlags.OpenProc) != 0)
 
297
                        {
 
298
                                plugin.openProc = new OpenProc(OpenProc);
 
299
                        }
 
300
                        if ((implementedMethods & MethodFlags.CloseProc) != 0)
 
301
                        {
 
302
                                plugin.closeProc = new CloseProc(CloseProc);
 
303
                        }
 
304
                        if ((implementedMethods & MethodFlags.PageCountProc) != 0)
 
305
                        {
 
306
                                plugin.pageCountProc = new PageCountProc(PageCountProc);
 
307
                        }
 
308
                        if ((implementedMethods & MethodFlags.PageCapabilityProc) != 0)
 
309
                        {
 
310
                                plugin.pageCapabilityProc = new PageCapabilityProc(PageCapabilityProc);
 
311
                        }
 
312
                        if ((implementedMethods & MethodFlags.LoadProc) != 0)
 
313
                        {
 
314
                                plugin.loadProc = new LoadProc(LoadProc);
 
315
                        }
 
316
                        if ((implementedMethods & MethodFlags.SaveProc) != 0)
 
317
                        {
 
318
                                plugin.saveProc = new SaveProc(SaveProc);
 
319
                        }
 
320
                        if ((implementedMethods & MethodFlags.ValidateProc) != 0)
 
321
                        {
 
322
                                plugin.validateProc = new ValidateProc(ValidateProc);
 
323
                        }
 
324
                        if ((implementedMethods & MethodFlags.MimeProc) != 0)
 
325
                        {
 
326
                                plugin.mimeProc = new MimeProc(MimeProc);
 
327
                        }
 
328
                        if ((implementedMethods & MethodFlags.SupportsExportBPPProc) != 0)
 
329
                        {
 
330
                                plugin.supportsExportBPPProc = new SupportsExportBPPProc(SupportsExportBPPProc);
 
331
                        }
 
332
                        if ((implementedMethods & MethodFlags.SupportsExportTypeProc) != 0)
 
333
                        {
 
334
                                plugin.supportsExportTypeProc = new SupportsExportTypeProc(SupportsExportTypeProc);
 
335
                        }
 
336
                        if ((implementedMethods & MethodFlags.SupportsICCProfilesProc) != 0)
 
337
                        {
 
338
                                plugin.supportsICCProfilesProc = new SupportsICCProfilesProc(SupportsICCProfilesProc);
 
339
                        }
 
340
 
 
341
                        // FormatProc is always implemented
 
342
                        plugin.formatProc = new FormatProc(FormatProc);
 
343
 
 
344
                        // InitProc is the register call back.
 
345
                        initProc = new InitProc(RegisterProc);
 
346
 
 
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;
 
349
                        if (registered)
 
350
                        {
 
351
                                PluginRepository.RegisterLocalPlugin(this);
 
352
                        }
 
353
                }
 
354
 
 
355
                private void RegisterProc(ref Plugin plugin, int format_id)
 
356
                {
 
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;
 
361
                }
 
362
 
 
363
                /// <summary>
 
364
                /// Gets or sets if the plugin is enabled.
 
365
                /// </summary>
 
366
                public bool Enabled
 
367
                {
 
368
                        get
 
369
                        {
 
370
                                if (registered)
 
371
                                {
 
372
                                        return (FreeImage.IsPluginEnabled(format) > 0);
 
373
                                }
 
374
                                else
 
375
                                {
 
376
                                        throw new ObjectDisposedException("plugin not registered");
 
377
                                }
 
378
                        }
 
379
                        set
 
380
                        {
 
381
                                if (registered)
 
382
                                {
 
383
                                        FreeImage.SetPluginEnabled(format, value);
 
384
                                }
 
385
                                else
 
386
                                {
 
387
                                        throw new ObjectDisposedException("plugin not registered");
 
388
                                }
 
389
                        }
 
390
                }
 
391
 
 
392
                /// <summary>
 
393
                /// Gets if the plugin was registered successfully.
 
394
                /// </summary>
 
395
                public bool Registered
 
396
                {
 
397
                        get { return registered; }
 
398
                }
 
399
 
 
400
                /// <summary>
 
401
                /// Gets the <see cref="FREE_IMAGE_FORMAT"/> FreeImage assigned to this plugin.
 
402
                /// </summary>
 
403
                public FREE_IMAGE_FORMAT Format
 
404
                {
 
405
                        get
 
406
                        {
 
407
                                return format;
 
408
                        }
 
409
                }
 
410
 
 
411
                /// <summary>
 
412
                /// Reads from an unmanaged stream.
 
413
                /// </summary>
 
414
                protected unsafe int Read(FreeImageIO io, fi_handle handle, uint size, uint count, ref byte[] buffer)
 
415
                {
 
416
                        fixed (byte* ptr = buffer)
 
417
                        {
 
418
                                return (int)io.readProc(new IntPtr(ptr), size, count, handle);
 
419
                        }
 
420
                }
 
421
 
 
422
                /// <summary>
 
423
                /// Reads a single byte from an unmanaged stream.
 
424
                /// </summary>
 
425
                protected unsafe int ReadByte(FreeImageIO io, fi_handle handle)
 
426
                {
 
427
                        byte buffer = 0;
 
428
                        return (int)io.readProc(new IntPtr(&buffer), 1, 1, handle) > 0 ? buffer : -1;
 
429
                }
 
430
 
 
431
                /// <summary>
 
432
                /// Writes to an unmanaged stream.
 
433
                /// </summary>
 
434
                protected unsafe int Write(FreeImageIO io, fi_handle handle, uint size, uint count, ref byte[] buffer)
 
435
                {
 
436
                        fixed (byte* ptr = buffer)
 
437
                        {
 
438
                                return (int)io.writeProc(new IntPtr(ptr), size, count, handle);
 
439
                        }
 
440
                }
 
441
 
 
442
                /// <summary>
 
443
                /// Writes a single byte to an unmanaged stream.
 
444
                /// </summary>
 
445
                protected unsafe int WriteByte(FreeImageIO io, fi_handle handle, byte value)
 
446
                {
 
447
                        return (int)io.writeProc(new IntPtr(&value), 1, 1, handle);
 
448
                }
 
449
 
 
450
                /// <summary>
 
451
                /// Seeks in an unmanaged stream.
 
452
                /// </summary>
 
453
                protected int Seek(FreeImageIO io, fi_handle handle, int offset, SeekOrigin origin)
 
454
                {
 
455
                        return io.seekProc(handle, offset, origin);
 
456
                }
 
457
 
 
458
                /// <summary>
 
459
                /// Retrieves the position of an unmanaged stream.
 
460
                /// </summary>
 
461
                protected int Tell(FreeImageIO io, fi_handle handle)
 
462
                {
 
463
                        return io.tellProc(handle);
 
464
                }
 
465
        }
 
466
}
 
 
b'\\ No newline at end of file'