1 module gfm.freeimage.freeimage;
2 
3 import std.conv,
4        std.string;
5 
6 import derelict.freeimage.freeimage,
7        derelict.util.exception;
8 
9 /// The one exception type thrown in this wrapper.
10 /// A failing FreeImage function should <b>always</b> throw an FreeImageException.
11 class FreeImageException : Exception
12 {
13     public
14     {
15         @safe pure nothrow this(string message, string file =__FILE__, size_t line = __LINE__, Throwable next = null)
16         {
17             super(message, file, line, next);
18         }
19     }
20 }
21 
22 /// FreeImage library wrapper.
23 final class FreeImage
24 {
25     public
26     {
27         /// Loads the FreeImage library and logs some information.
28         /// Throws: FreeImageException on error.
29         this(bool useExternalPlugins = false)
30         {
31             try
32             {
33                 DerelictFI.load();
34             }
35             catch(DerelictException e)
36             {
37                 throw new FreeImageException(e.msg);
38             }
39 
40             //FreeImage_Initialise(useExternalPlugins ? TRUE : FALSE); // documentation says it's useless
41             _libInitialized = true;
42         }
43 
44         ~this()
45         {
46             close();
47         }
48 
49         void close()
50         {
51             if (_libInitialized)
52             {
53                 //FreeImage_DeInitialise(); // documentation says it's useless
54                 DerelictFI.unload();
55                 _libInitialized = false;
56             }
57         }
58 
59         const(char)[] getVersion()
60         {
61             const(char)* versionZ = FreeImage_GetVersion();
62             return fromStringz(versionZ);
63         }
64 
65         const(char)[] getCopyrightMessage()
66         {
67             const(char)* copyrightZ = FreeImage_GetCopyrightMessage();
68             return fromStringz(copyrightZ);
69         }
70     }
71 
72     private
73     {
74         bool _libInitialized;
75     }
76 }