D bindings to the GraphicsMagick library.
Revisión | 9fe65bb610ffa90280b0ae3b8026bebfc9a070b5 (tree) |
---|---|
Tiempo | 2023-05-27 11:04:51 |
Autor | Mio <stigma@disr...> |
Commiter | Mio |
[magickd] Change how loading GraphicsMagick works
@@ -55,29 +55,32 @@ This requires loading the library at runtime and binding the symbols. | ||
55 | 55 | import magickd; |
56 | 56 | |
57 | 57 | /* Attempt to load the GraphicsMagick library. */ |
58 | -void *libgm = null; | |
59 | - | |
60 | -bool success = loadGraphicsMagick(libgm); | |
61 | -if (false == success) { | |
62 | - /* | |
63 | - * We failed to load for some reason (errors are printed to stderr) | |
64 | - */ | |
65 | - if (null is libgm) { | |
66 | - /* | |
67 | - * We failed to find the shared library, perhaps GraphicsMagick | |
68 | - * is not installed. | |
69 | - */ | |
70 | - } else { | |
71 | - /* | |
72 | - * We just had some issues linking some of the dynamic functions, | |
73 | - * so proceed with caution. Again, any issues are printed to | |
74 | - * stderr. | |
75 | - */ | |
76 | - } | |
77 | -} else { | |
78 | - /* | |
79 | - * We loaded the library without any issues! | |
80 | - */ | |
58 | +MDLoadStatus result = loadGraphicsMagick(); | |
59 | + | |
60 | +/* Do we need to unload the library? */ | |
61 | +bool unload = false; | |
62 | + | |
63 | +switch (result) { | |
64 | + case MDLoadStatus.success: | |
65 | + // All Good! | |
66 | + unload = true; | |
67 | + break; | |
68 | + case MDLoadStatus.badLibrary: | |
69 | + // We have loaded the GraphicsMagick library, but some of the symbols | |
70 | + // that we expected to find, could not be found. | |
71 | + // Something may not work. | |
72 | + unload = true; | |
73 | + break; | |
74 | + case MDLoadStatus.noLibrary: | |
75 | + // We couldn't find the GraphicsMagick library on the system. | |
76 | + // Nothing will work. | |
77 | + break; | |
78 | + default: | |
79 | + assert(0); | |
80 | +} | |
81 | + | |
82 | +if (unload) { | |
83 | + unloadGraphicsMagick(); | |
81 | 84 | } |
82 | 85 | ``` |
83 | 86 |
@@ -0,0 +1,99 @@ | ||
1 | +/* | |
2 | + File: magickd/initialisation.d | |
3 | + Contains: Functions related to initialsing the GraphicsMagick C library. | |
4 | + Copyright: (C) 2023 Mio | |
5 | + | |
6 | + Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | + of this software and associated documentation files (the "Software"), to deal | |
8 | + in the Software without restriction, including without limitation the rights | |
9 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | + copies of the Software, and to permit persons to whom the Software is | |
11 | + furnished to do so, subject to the following conditions: | |
12 | + | |
13 | + The above copyright notice and this permission notice shall be included in all | |
14 | + copies or substantial portions of the Software. | |
15 | + | |
16 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
19 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
22 | + SOFTWARE. | |
23 | +*/ | |
24 | +module magickd.initialisation; | |
25 | + | |
26 | +/// An enumeration for the potential return values of `loadGraphicsMagick`. | |
27 | +enum MDLoadStatus | |
28 | +{ | |
29 | + /// Loading was successful. | |
30 | + success, | |
31 | + /// Some symbols could not be found in the libraries. | |
32 | + badLibrary, | |
33 | + /// We couldn't find all of the GraphicsMagick libraries. | |
34 | + noLibrary, | |
35 | +} | |
36 | + | |
37 | +/// Load the GraphicsMagick/GraphicsMagickWand libraries. | |
38 | +/// | |
39 | +/// Params: | |
40 | +/// autoload = Should we automatically initialise the GraphicsMagick library | |
41 | +/// via `InitializeMagick`. | |
42 | +/// | |
43 | +/// Returns: | |
44 | +/// A value of `MDLoadStatus`, depending on how successful the loading of the | |
45 | +/// library was. | |
46 | +/// | |
47 | +/// If loading was successful, then `MDLoadStatus.success` is returned. | |
48 | +/// | |
49 | +/// If the libraries (GraphicsMagick and GraphicsMagickWand) could not be | |
50 | +/// found on the system, then `MDLoadStatus.noLibrary` is returned. | |
51 | +/// | |
52 | +/// `MDLoadStatus.badLibrary` is returned when the libraries *could* be found, | |
53 | +/// but one or more of the symbols could not be found. | |
54 | +/// | |
55 | +/// **NOTE**: When using the static version of magickd, this will always | |
56 | +/// return `MDLoadStatus.success`. | |
57 | +MDLoadStatus loadGraphicsMagick(bool autoload = true) | |
58 | +{ | |
59 | + import graphicsmagick_c.magick.magick : InitializeMagick; | |
60 | + | |
61 | + version (GMagick_Dynamic) { | |
62 | + import graphicsmagick_c.config : _loadGraphicsMagick = loadGraphicsMagick, | |
63 | + loadGraphicsMagickWand; | |
64 | + | |
65 | + void* lib; | |
66 | + | |
67 | + bool success = _loadGraphicsMagick(lib); | |
68 | + if (null is lib) { | |
69 | + return MDLoadStatus.noLibrary; | |
70 | + } | |
71 | + | |
72 | + success &= loadGraphicsMagickWand(lib); | |
73 | + if (null is lib) { | |
74 | + return MDLoadStatus.noLibrary; | |
75 | + } | |
76 | + | |
77 | + if (autoload) { | |
78 | + InitializeMagick(null); | |
79 | + } | |
80 | + | |
81 | + return success ? MDLoadStatus.success : MDLoadStatus.badLibrary; | |
82 | + } else { | |
83 | + if (autoload) { | |
84 | + InitializeMagick(null); | |
85 | + } | |
86 | + | |
87 | + return MDLoadStatus.success; | |
88 | + } | |
89 | +} | |
90 | + | |
91 | +/// Unload the GraphcisMagick library. | |
92 | +/// | |
93 | +/// This is equivalent to calling `DestroyMagick`. | |
94 | +void unloadGraphicsMagick() | |
95 | +{ | |
96 | + import graphicsmagick_c.magick.magick : DestroyMagick; | |
97 | + | |
98 | + DestroyMagick(); | |
99 | +} |
@@ -1,7 +1,7 @@ | ||
1 | 1 | /* |
2 | 2 | File: magickd/package.d |
3 | 3 | Contains: Public imports for magickd. |
4 | - Copyright: (C) 2021, 2023 kaerou <stigma@disroot.org> | |
4 | + Copyright: (C) 2021, 2023 Mio | |
5 | 5 | |
6 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy |
7 | 7 | of this software and associated documentation files (the "Software"), to deal |
@@ -25,13 +25,10 @@ module magickd; | ||
25 | 25 | |
26 | 26 | public |
27 | 27 | { |
28 | - version (GMagick_Dynamic) { | |
29 | - // MagickD only uses GraphicsMagick (not GraphicsMagickWand) | |
30 | - import graphicsmagick_c.config : loadGraphicsMagick; | |
31 | - } | |
32 | - | |
33 | 28 | import graphicsmagick_c.magick.magick : InitializeMagick, DestroyMagick; |
34 | 29 | |
30 | + import magickd.initialisation; | |
31 | + | |
35 | 32 | import magickd.image_info; |
36 | 33 | import magickd.image; |
37 | 34 | import magickd.exception; |