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 import std.logger;
10 
11 import gfm.core.text;
12 
13 /// The one exception type thrown in this wrapper.
14 /// A failing FreeImage function should <b>always</b> throw an FreeImageException.
15 class FreeImageException : Exception
16 {
17     public
18     {
19         this(string msg)
20         {
21             super(msg);
22         }
23     }
24 }
25 
26 /// FreeImage library wrapper.
27 final class FreeImage
28 {
29     public
30     {
31         /// Loads the FreeImage library and logs some information.
32         /// Throws: FreeImageException on error.
33         this(Logger logger, bool useExternalPlugins = false)
34         {
35             _logger = logger is null ? new NullLogger() : logger;
36 
37             try
38             {
39                 DerelictFI.load();
40             }
41             catch(DerelictException e)
42             {
43                 throw new FreeImageException(e.msg);
44             }
45 
46             //FreeImage_Initialise(useExternalPlugins ? TRUE : FALSE); // documentation says it's useless
47             _libInitialized = true;
48 
49             _logger.infof("FreeImage %s initialized.", getVersion());
50             _logger.infof("%s.", getCopyrightMessage());
51         }
52 
53         ~this()
54         {
55             close();
56         }
57 
58         void close()
59         {
60             if (_libInitialized)
61             {
62                 //FreeImage_DeInitialise(); // documentation says it's useless
63                 DerelictFI.unload();
64                 _libInitialized = false;
65             }
66         }
67 
68         string getVersion()
69         {
70             const(char)* versionZ = FreeImage_GetVersion();
71             return sanitizeUTF8(versionZ, _logger, "FreeImage_GetVersion");
72         }
73 
74         string getCopyrightMessage()
75         {
76             const(char)* copyrightZ = FreeImage_GetCopyrightMessage();
77             return sanitizeUTF8(copyrightZ, _logger, "FreeImage_GetCopyrightMessage");
78         }
79     }
80 
81     package
82     {
83         Logger _logger;
84     }
85 
86     private
87     {
88         bool _libInitialized;
89     }
90 }