From: Christophe CURIS <christophe.cu...@free.fr>

Instead of calling 'MagickWandGenesis' every time an image is loaded, only
call it the first time, and similarly do not call 'MagickWandTerminus' if
the library has not been used before.

Signed-off-by: Christophe CURIS <christophe.cu...@free.fr>
---
 wrlib/load_magick.c | 34 ++++++++++++++++++++++++++++++++--
 1 file changed, 32 insertions(+), 2 deletions(-)

diff --git a/wrlib/load_magick.c b/wrlib/load_magick.c
index e8201b9..622276f 100644
--- a/wrlib/load_magick.c
+++ b/wrlib/load_magick.c
@@ -28,6 +28,9 @@
 #include "imgformat.h"
 
 
+static int RInitMagickIfNeeded(void);
+
+
 RImage *RLoadMagick(const char *file_name)
 {
        RImage *image = NULL;
@@ -38,7 +41,10 @@ RImage *RLoadMagick(const char *file_name)
        MagickBooleanType hasAlfa;
        PixelWand *bg_wand = NULL;
 
-       MagickWandGenesis();
+       if (RInitMagickIfNeeded()) {
+               RErrorCode = RERR_BADFORMAT;
+               return NULL;
+       }
 
        /* Create a wand */
        m_wand = NewMagickWand();
@@ -87,7 +93,31 @@ bye:
        return image;
 }
 
+/* Track the state of the library in memory */
+static enum {
+       MW_NotReady,
+       MW_Ready
+} magick_state;
+
+/*
+ * Initialise MagickWand, but only if it was not already done
+ *
+ * Return ok(0) when MagickWand is usable and fail(!0) if not usable
+ */
+static int RInitMagickIfNeeded(void)
+{
+       if (magick_state == MW_NotReady) {
+               MagickWandGenesis();
+               magick_state = MW_Ready;
+       }
+
+       return 0;
+}
+
 void RReleaseMagick(void)
 {
-       MagickWandTerminus();
+       if (magick_state == MW_Ready) {
+               MagickWandTerminus();
+               magick_state = MW_NotReady;
+       }
 }
-- 
1.9.2


-- 
To unsubscribe, send mail to wmaker-dev-unsubscr...@lists.windowmaker.org.

Reply via email to