• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

D bindings to the GraphicsMagick library.


Commit MetaInfo

Revisión9fe65bb610ffa90280b0ae3b8026bebfc9a070b5 (tree)
Tiempo2023-05-27 11:04:51
AutorMio <stigma@disr...>
CommiterMio

Log Message

[magickd] Change how loading GraphicsMagick works

Cambiar Resumen

Diferencia incremental

--- a/README.md
+++ b/README.md
@@ -55,29 +55,32 @@ This requires loading the library at runtime and binding the symbols.
5555 import magickd;
5656
5757 /* 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();
8184 }
8285 ```
8386
--- /dev/null
+++ b/source/magickd/initialisation.d
@@ -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+}
--- a/source/magickd/package.d
+++ b/source/magickd/package.d
@@ -1,7 +1,7 @@
11 /*
22 File: magickd/package.d
33 Contains: Public imports for magickd.
4- Copyright: (C) 2021, 2023 kaerou <stigma@disroot.org>
4+ Copyright: (C) 2021, 2023 Mio
55
66 Permission is hereby granted, free of charge, to any person obtaining a copy
77 of this software and associated documentation files (the "Software"), to deal
@@ -25,13 +25,10 @@ module magickd;
2525
2626 public
2727 {
28- version (GMagick_Dynamic) {
29- // MagickD only uses GraphicsMagick (not GraphicsMagickWand)
30- import graphicsmagick_c.config : loadGraphicsMagick;
31- }
32-
3328 import graphicsmagick_c.magick.magick : InitializeMagick, DestroyMagick;
3429
30+ import magickd.initialisation;
31+
3532 import magickd.image_info;
3633 import magickd.image;
3734 import magickd.exception;